2020-05-20 05:08:11 -04:00
---
stage: Package
group: Package
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-05-20 05:08:11 -04:00
---
2021-02-16 13:09:24 -05:00
# npm packages in the Package Registry **(FREE)**
2019-09-17 08:06:48 -04:00
2022-01-07 16:17:37 -05:00
> [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) from GitLab Premium to GitLab Free in 13.3.
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
Publish npm packages in your project's Package Registry. Then install the
2020-11-04 16:08:43 -05:00
packages whenever you need to use them as a dependency.
2019-09-17 08:06:48 -04:00
2020-12-03 19:09:55 -05:00
Only [scoped ](https://docs.npmjs.com/misc/scope/ ) packages are supported.
2019-09-17 08:06:48 -04:00
2021-05-18 20:10:43 -04:00
For documentation of the specific API endpoints that the npm package manager
client uses, see the [npm API documentation ](../../../api/packages/npm.md ).
2021-11-30 16:10:33 -05:00
WARNING:
Never hardcode GitLab tokens (or any tokens) directly in `.npmrc` files or any other files that can
be committed to a repository.
2021-02-12 22:08:45 -05:00
## Build an npm package
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
This section covers how to install npm or Yarn and build a package for your
2020-11-04 16:08:43 -05:00
JavaScript project.
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
If you already use npm and know how to build your own packages, go to
2020-11-04 16:08:43 -05:00
the [next section ](#authenticate-to-the-package-registry ).
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
### Install npm
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
Install Node.js and npm in your local development environment by following
2020-12-03 19:09:55 -05:00
the instructions at [npmjs.com ](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm/ ).
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
When installation is complete, verify you can use npm in your terminal by
2020-01-27 07:08:35 -05:00
running:
2019-12-05 16:07:40 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-01-27 07:08:35 -05:00
npm --version
2019-12-05 16:07:40 -05:00
```
2020-01-27 07:08:35 -05:00
2021-02-12 22:08:45 -05:00
The npm version is shown in the output:
2020-01-27 07:08:35 -05:00
2020-02-28 22:07:51 -05:00
```plaintext
2020-01-27 07:08:35 -05:00
6.10.3
2019-12-05 16:07:40 -05:00
```
2020-11-04 16:08:43 -05:00
### Install Yarn
2019-12-05 16:07:40 -05:00
2021-02-12 22:08:45 -05:00
As an alternative to npm, you can install Yarn in your local environment by following the
2021-04-23 17:09:46 -04:00
instructions at [classic.yarnpkg.com ](https://classic.yarnpkg.com/en/docs/install ).
2019-12-05 16:07:40 -05:00
2020-11-04 16:08:43 -05:00
When installation is complete, verify you can use Yarn in your terminal by
running:
2019-10-31 11:06:41 -04:00
2020-01-30 10:09:15 -05:00
```shell
2020-01-27 07:08:35 -05:00
yarn --version
```
2020-11-04 16:08:43 -05:00
The Yarn version is shown in the output:
2020-01-27 07:08:35 -05:00
2020-02-28 22:07:51 -05:00
```plaintext
2020-01-27 07:08:35 -05:00
1.19.1
```
2020-11-04 16:08:43 -05:00
### Create a project
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
To create a project:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
1. Create an empty directory.
1. Go to the directory and initialize an empty package by running:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
```shell
npm init
```
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
Or if you're using Yarn:
```shell
yarn init
```
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
1. Enter responses to the questions. Ensure the **package name** follows
the [naming convention ](#package-naming-convention ) and is scoped to the
project or group where the registry exists.
2020-01-27 07:08:35 -05:00
2020-11-11 07:09:06 -05:00
A `package.json` file is created.
2021-02-12 22:08:45 -05:00
## Use the GitLab endpoint for npm packages
2020-11-11 07:09:06 -05:00
2021-02-12 22:08:45 -05:00
To use the GitLab endpoint for npm packages, choose an option:
2020-11-11 07:09:06 -05:00
2021-02-12 22:08:45 -05:00
- **Project-level**: Use when you have few npm packages and they are not in
2021-02-19 19:10:55 -05:00
the same GitLab group. The [package naming convention ](#package-naming-convention ) is not enforced at this level.
2021-07-02 14:08:28 -04:00
Instead, you should use a [scope ](https://docs.npmjs.com/cli/v6/using-npm/scope/ ) for your package.
2021-02-17 13:09:19 -05:00
When you use a scope, the registry URL is [updated ](#authenticate-to-the-package-registry ) only for that scope.
2021-02-12 22:08:45 -05:00
- **Instance-level**: Use when you have many npm packages in different
2020-11-11 07:09:06 -05:00
GitLab groups or in their own namespace. Be sure to comply with the [package naming convention ](#package-naming-convention ).
Some features such as [publishing ](#publish-an-npm-package ) a package is only available on the project-level endpoint.
2019-09-17 08:06:48 -04:00
2020-11-04 16:08:43 -05:00
## Authenticate to the Package Registry
2019-09-17 08:06:48 -04:00
2021-01-07 16:10:18 -05:00
You must authenticate with the Package Registry when the project
is private. Public projects do not require authentication.
To authenticate, use one of the following:
2019-09-17 08:06:48 -04:00
2020-11-04 16:08:43 -05:00
- A [personal access token ](../../../user/profile/personal_access_tokens.md )
(required for two-factor authentication (2FA)), with the scope set to `api` .
2020-11-08 22:09:03 -05:00
- A [deploy token ](../../project/deploy_tokens/index.md ), with the scope set to `read_package_registry` , `write_package_registry` , or both.
2020-11-04 16:08:43 -05:00
- It's not recommended, but you can use [OAuth tokens ](../../../api/oauth2.md#resource-owner-password-credentials-flow ).
2021-02-12 22:08:45 -05:00
Standard OAuth tokens cannot authenticate to the GitLab npm Registry. You must use a personal access token with OAuth headers.
2020-11-04 16:08:43 -05:00
- A [CI job token ](#authenticate-with-a-ci-job-token ).
2021-06-04 11:10:25 -04:00
- Your npm package name must be in the format of [`@scope/package-name` ](#package-naming-convention ).
It must match exactly, including the case.
2019-09-17 08:06:48 -04:00
2020-11-04 16:08:43 -05:00
### Authenticate with a personal access token or deploy token
2019-09-17 08:06:48 -04:00
2020-11-25 13:09:53 -05:00
To authenticate with the Package Registry, you need a [personal access token ](../../profile/personal_access_tokens.md ) or [deploy token ](../../project/deploy_tokens/index.md ).
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
#### Project-level npm endpoint
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
To use the [project-level ](#use-the-gitlab-endpoint-for-npm-packages ) npm endpoint, set your npm configuration:
2020-11-11 07:09:06 -05:00
```shell
# Set URL for your scoped packages.
# For example package with name `@foo/bar` will use this URL for download
npm config set @foo:registry https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/
2019-09-17 08:06:48 -04:00
2020-11-11 07:09:06 -05:00
# Add the token for the scoped packages URL. Replace <your_project_id>
# with the project where your package is located.
2021-03-24 14:09:31 -04:00
npm config set -- '//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken' "< your_token > "
2019-09-17 08:06:48 -04:00
```
2020-11-04 16:08:43 -05:00
- `<your_project_id>` is your project ID, found on the project's home page.
- `<your_token>` is your personal access token or deploy token.
- Replace `gitlab.example.com` with your domain name.
2021-02-12 22:08:45 -05:00
You should now be able to publish and install npm packages in your project.
2020-11-04 16:08:43 -05:00
If you encounter an error with [Yarn ](https://classic.yarnpkg.com/en/ ), view
[troubleshooting steps ](#troubleshooting ).
2021-02-12 22:08:45 -05:00
#### Instance-level npm endpoint
2020-11-11 07:09:06 -05:00
2021-02-12 22:08:45 -05:00
To use the [instance-level ](#use-the-gitlab-endpoint-for-npm-packages ) npm endpoint, set your npm configuration:
2020-11-11 07:09:06 -05:00
```shell
# Set URL for your scoped packages.
# For example package with name `@foo/bar` will use this URL for download
npm config set @foo:registry https://gitlab.example.com/api/v4/packages/npm/
# Add the token for the scoped packages URL. This will allow you to download
# `@foo/` packages from private projects.
2021-03-24 14:09:31 -04:00
npm config set -- '//gitlab.example.com/api/v4/packages/npm/:_authToken' "< your_token > "
2020-11-11 07:09:06 -05:00
```
- `<your_token>` is your personal access token or deploy token.
- Replace `gitlab.example.com` with your domain name.
2021-03-05 13:09:17 -05:00
You should now be able to install npm packages in your project.
2020-11-11 07:09:06 -05:00
If you encounter an error with [Yarn ](https://classic.yarnpkg.com/en/ ), view
[troubleshooting steps ](#troubleshooting ).
2020-11-04 16:08:43 -05:00
### Authenticate with a CI job token
2019-09-17 08:06:48 -04:00
2021-11-17 16:13:37 -05:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/9104) in GitLab 12.5.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) from GitLab Premium to GitLab Free in 13.3.
2020-11-04 16:08:43 -05:00
2021-02-12 22:08:45 -05:00
If you're using npm with GitLab CI/CD, a CI job token can be used instead of a personal access token or deploy token.
2020-11-04 16:08:43 -05:00
The token inherits the permissions of the user that generates the pipeline.
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
#### Project-level npm endpoint
2020-11-11 07:09:06 -05:00
2021-02-12 22:08:45 -05:00
To use the [project-level ](#use-the-gitlab-endpoint-for-npm-packages ) npm endpoint, add a corresponding section to your `.npmrc` file:
2020-11-11 07:09:06 -05:00
```ini
@foo:registry =https://gitlab.example.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/
//gitlab.example.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}
```
2021-02-12 22:08:45 -05:00
#### Instance-level npm endpoint
2020-11-11 07:09:06 -05:00
2021-02-12 22:08:45 -05:00
To use the [instance-level ](#use-the-gitlab-endpoint-for-npm-packages ) npm endpoint, add a corresponding section to your `.npmrc` file:
2019-09-17 08:06:48 -04:00
2020-11-04 16:08:43 -05:00
```ini
@foo:registry =https://gitlab.example.com/api/v4/packages/npm/
//gitlab.example.com/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}
```
2019-09-17 08:06:48 -04:00
2020-11-04 16:08:43 -05:00
#### Use variables to avoid hard-coding auth token values
2019-09-19 14:06:18 -04:00
2020-11-04 16:08:43 -05:00
To avoid hard-coding the `authToken` value, you may use a variable in its place:
2019-09-19 14:06:18 -04:00
2020-01-30 10:09:15 -05:00
```shell
2021-03-24 14:09:31 -04:00
npm config set -- '//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken' "${NPM_TOKEN}"
npm config set -- '//gitlab.example.com/api/v4/packages/npm/:_authToken' "${NPM_TOKEN}"
2019-09-19 14:06:18 -04:00
```
2020-11-04 16:08:43 -05:00
Then, you can run `npm publish` either locally or by using GitLab CI/CD.
2019-09-19 14:06:18 -04:00
- **Locally:** Export `NPM_TOKEN` before publishing:
2020-01-30 10:09:15 -05:00
```shell
2019-09-19 14:06:18 -04:00
NPM_TOKEN=< your_token > npm publish
```
2021-06-28 11:08:03 -04:00
- **GitLab CI/CD:** Set an `NPM_TOKEN` [CI/CD variable ](../../../ci/variables/index.md )
2019-09-19 14:06:18 -04:00
under your project's **Settings > CI/CD > Variables** .
2019-12-10 02:53:40 -05:00
2021-11-10 13:12:35 -05:00
## Working with private registries
When working with private repositories, you may want to configure additional settings to ensure a secure communication channel:
```shell
# Force npm to always require authentication when accessing the registry, even for GET requests.
npm config set always-auth true
```
2020-11-04 16:08:43 -05:00
## Package naming convention
2019-11-15 04:06:30 -05:00
2021-02-17 13:09:19 -05:00
When you use the [instance-level endpoint ](#use-the-gitlab-endpoint-for-npm-packages ), only the packages with names in the format of `@scope/package-name` are available.
2019-11-15 04:06:30 -05:00
2021-02-22 19:11:11 -05:00
- The `@scope` is the root namespace of the GitLab project. To follow npm's convention, it should be
lowercase. However, the GitLab package registry allows for uppercase. Before GitLab 13.10, the
`@scope` had to be a case-sensitive match of the GitLab project's root namespace. This was
problematic because the npm public registry does not allow uppercase letters. GitLab 13.10 relaxes
this requirement and translates uppercase in the GitLab `@scope` to lowercase for npm. For
example, a package `@MyScope/package-name` in GitLab becomes `@myscope/package-name` for npm.
2020-11-04 16:08:43 -05:00
- The `package-name` can be whatever you want.
2019-11-15 04:06:30 -05:00
2020-11-04 16:08:43 -05:00
For example, if your project is `https://gitlab.example.com/my-org/engineering-group/team-amazing/analytics` ,
the root namespace is `my-org` . When you publish a package, it must have `my-org` as the scope.
2019-11-15 04:06:30 -05:00
2020-11-04 16:08:43 -05:00
| Project | Package | Supported |
| ---------------------- | ----------------------- | --------- |
| `my-org/bar` | `@my-org/bar` | Yes |
| `my-org/bar/baz` | `@my-org/baz` | Yes |
2021-02-22 19:11:11 -05:00
| `My-Org/Bar/baz` | `@my-org/Baz` | Yes |
| `My-Org/Bar/baz` | `@My-Org/Baz` | Yes |
2020-11-04 16:08:43 -05:00
| `my-org/bar/buz` | `@my-org/anything` | Yes |
| `gitlab-org/gitlab` | `@gitlab-org/gitlab` | Yes |
| `gitlab-org/gitlab` | `@foo/bar` | No |
In GitLab, this regex validates all package names from all package managers:
```plaintext
/\A\@?(([\w\-\.\+]*)\/)*([\w\-\.]+)@?(([\w\-\.\+]*)\/)*([\w\-\.]*)\z/
2019-11-15 04:06:30 -05:00
```
2019-09-19 14:06:18 -04:00
2021-02-12 22:08:45 -05:00
This regex allows almost all of the characters that npm allows, with a few exceptions (for example, `~` is not allowed).
2019-09-17 08:06:48 -04:00
2021-02-22 19:11:11 -05:00
The regex also allows for capital letters, while npm does not.
2020-11-04 16:08:43 -05:00
2022-01-21 16:16:35 -05:00
## Limitations
2020-11-04 16:08:43 -05:00
When you update the path of a user or group, or transfer a subgroup or project,
2021-02-12 22:08:45 -05:00
you must remove any npm packages first. You cannot update the root namespace
of a project with npm packages. Make sure you update your `.npmrc` files to follow
2020-11-04 16:08:43 -05:00
the naming convention and run `npm publish` if necessary.
2020-10-26 20:08:28 -04:00
2021-02-12 22:08:45 -05:00
## Publish an npm package
2020-11-04 16:08:43 -05:00
2020-11-11 07:09:06 -05:00
Prerequisites:
2019-09-17 08:06:48 -04:00
2020-11-11 07:09:06 -05:00
- [Authenticate ](#authenticate-to-the-package-registry ) to the Package Registry.
2021-02-12 22:08:45 -05:00
- Set a [project-level npm endpoint ](#use-the-gitlab-endpoint-for-npm-packages ).
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
To upload an npm package to your project, run this command:
2019-09-17 08:06:48 -04:00
2020-01-30 10:09:15 -05:00
```shell
2019-09-17 08:06:48 -04:00
npm publish
```
2020-11-04 16:08:43 -05:00
To view the package, go to your project's **Packages & Registries** .
2019-09-17 08:06:48 -04:00
2021-03-22 14:09:24 -04:00
You can also define `"publishConfig"` for your project in `package.json` . For example:
```json
{
"publishConfig": { "@foo:registry":" https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/" }
}
```
This forces the package to publish only to the specified registry.
2020-11-04 16:08:43 -05:00
If you try to publish a package [with a name that already exists ](#publishing-packages-with-the-same-name-or-version ) within
a given scope, you get a `403 Forbidden!` error.
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
## Publish an npm package by using CI/CD
2019-09-17 08:06:48 -04:00
2020-11-11 07:09:06 -05:00
Prerequisites:
- [Authenticate ](#authenticate-to-the-package-registry ) to the Package Registry.
2021-02-12 22:08:45 -05:00
- Set a [project-level npm endpoint ](#use-the-gitlab-endpoint-for-npm-packages ).
2021-06-04 11:10:25 -04:00
- Your npm package name must be in the format of [`@scope/package-name` ](#package-naming-convention ).
2021-01-21 19:09:25 -05:00
It must match exactly, including the case. This is different than the
2021-02-12 22:08:45 -05:00
npm naming convention, but it is required to work with the GitLab Package Registry.
2020-11-11 07:09:06 -05:00
2021-06-28 11:08:03 -04:00
To work with npm commands within [GitLab CI/CD ](../../../ci/index.md ), you can use
2020-11-04 16:08:43 -05:00
`CI_JOB_TOKEN` in place of the personal access token or deploy token in your commands.
2020-01-27 07:08:35 -05:00
2021-02-12 22:08:45 -05:00
An example `.gitlab-ci.yml` file for publishing npm packages:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
```yaml
image: node:latest
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
stages:
- deploy
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
deploy:
stage: deploy
script:
2021-05-24 14:10:28 -04:00
- echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">.npmrc
2020-11-04 16:08:43 -05:00
- npm publish
2020-01-27 07:08:35 -05:00
```
2020-12-01 16:09:29 -05:00
See the
2021-02-12 22:08:45 -05:00
[Publish npm packages to the GitLab Package Registry using semantic-release ](../../../ci/examples/semantic-release.md )
2020-12-01 16:09:29 -05:00
step-by-step guide and demo project for a complete example.
2021-04-23 17:09:46 -04:00
## Configure the GitLab npm registry with Yarn 2
2021-07-02 14:08:28 -04:00
You can get started with Yarn 2 by following the [Yarn documentation ](https://yarnpkg.com/getting-started/install/ ).
2021-04-23 17:09:46 -04:00
To publish and install with the project-level npm endpoint, set the following configuration in
`.yarnrc.yml` :
```yaml
npmScopes:
foo:
npmRegistryServer: "https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/"
npmPublishRegistry: "https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/"
2021-04-29 08:09:58 -04:00
2021-04-23 17:09:46 -04:00
npmRegistries:
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:
npmAlwaysAuth: true
npmAuthToken: "< your_token > "
```
For the instance-level npm endpoint, use this Yarn 2 configuration in `.yarnrc.yml` :
```yaml
npmScopes:
foo:
npmRegistryServer: "https://gitlab.example.com/api/v4/packages/npm/"
npmRegistries:
//gitlab.example.com/api/v4/packages/npm/:
npmAlwaysAuth: true
npmAuthToken: "< your_token > "
```
In this configuration:
- Replace `<your_token>` with your personal access token or deploy token.
- Replace `<your_project_id>` with your project's ID, which you can find on the project's home page.
- Replace `gitlab.example.com` with your domain name.
- Your scope is `foo` , without `@` .
2020-11-04 16:08:43 -05:00
## Publishing packages with the same name or version
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
You cannot publish a package if a package of the same name and version already exists.
You must delete the existing package first.
2020-01-27 07:08:35 -05:00
2021-08-11 05:10:00 -04:00
This rule has a different impact depending on the package name:
- For packages following the [naming convention ](#package-naming-convention ), you can't publish a
package with a duplicate name and version to the root namespace.
- For packages not following the [naming convention ](#package-naming-convention ), you can't publish
a package with a duplicate name and version to the project you target with the upload.
2020-11-04 16:08:43 -05:00
This aligns with npmjs.org's behavior. However, npmjs.org does not ever let you publish
the same version more than once, even if it has been deleted.
2020-01-27 07:08:35 -05:00
2021-11-10 13:12:35 -05:00
## `package.json` limitations
You can't publish a package if its `package.json` file exceeds 20,000 characters.
2020-11-04 16:08:43 -05:00
## Install a package
2020-01-27 07:08:35 -05:00
2021-02-12 22:08:45 -05:00
npm packages are commonly-installed by using the `npm` or `yarn` commands
2021-02-17 13:09:19 -05:00
in a JavaScript project. You can install a package from the scope of a project or instance.
If multiple packages have the same name and version, when you install a package, the most recently-published package is retrieved.
2020-01-27 07:08:35 -05:00
2021-09-23 11:11:29 -04:00
1. Set the URL for scoped packages.
For [instance-level endpoints ](#use-the-gitlab-endpoint-for-npm-packages ) run:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
```shell
npm config set @foo:registry https://gitlab.example.com/api/v4/packages/npm/
```
2020-01-27 07:08:35 -05:00
2021-09-23 11:11:29 -04:00
- Replace `@foo` with your scope.
- Replace `gitlab.example.com` with your domain name.
For [project-level endpoints ](#use-the-gitlab-endpoint-for-npm-packages ) run:
```shell
npm config set @foo:registry https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/
```
- Replace `@foo` with your scope.
- Replace `gitlab.example.com` with your domain name.
- Replace `<your_project_id>` with your project ID, found on the project's home page.
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
1. Ensure [authentication ](#authenticate-to-the-package-registry ) is configured.
2020-11-11 07:09:06 -05:00
2021-01-21 19:09:25 -05:00
1. To install a package in your project, run:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
```shell
2021-01-21 19:09:25 -05:00
npm install @my -scope/my-package
2020-11-04 16:08:43 -05:00
```
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
Or if you're using Yarn:
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
```shell
2021-01-21 19:09:25 -05:00
yarn add @my -scope/my-package
2020-11-04 16:08:43 -05:00
```
2020-03-11 11:09:37 -04:00
2020-11-04 16:08:43 -05:00
In [GitLab 12.9 and later ](https://gitlab.com/gitlab-org/gitlab/-/issues/55344 ),
2021-02-12 22:08:45 -05:00
when an npm package is not found in the Package Registry, the request is forwarded to [npmjs.com ](https://www.npmjs.com/ ).
2020-03-11 11:09:37 -04:00
Administrators can disable this behavior in the [Continuous Integration settings ](../../admin_area/settings/continuous_integration.md ).
2021-02-12 22:08:45 -05:00
### Install npm packages from other organizations
2020-09-23 17:09:28 -04:00
You can route package requests to organizations and users outside of GitLab.
2020-11-04 16:08:43 -05:00
To do this, add lines to your `.npmrc` file. Replace `my-org` with the namespace or group that owns your project's repository,
and use your organization's URL. The name is case-sensitive and must match the name of your group or namespace exactly.
2020-09-23 17:09:28 -04:00
2021-11-30 16:10:33 -05:00
Use environment variables to set up your tokens: `export MY_TOKEN="<your token>"` .
2020-09-23 17:09:28 -04:00
```shell
@foo:registry =https://gitlab.example.com/api/v4/packages/npm/
2021-11-30 16:10:33 -05:00
//gitlab.example.com/api/v4/packages/npm/:_authToken=${MY_TOKEN}
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken=${MY_TOKEN}
2020-09-23 17:09:28 -04:00
@my -other-org:registry=https://gitlab.example.com/api/v4/packages/npm/
2021-11-30 16:10:33 -05:00
//gitlab.example.com/api/v4/packages/npm/:_authToken=${MY_TOKEN}
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken=${MY_TOKEN}
2020-09-23 17:09:28 -04:00
```
2021-11-10 13:12:35 -05:00
### npm metadata
2020-01-27 07:08:35 -05:00
2021-11-17 16:13:37 -05:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11867) in GitLab 12.6.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) from GitLab Premium to GitLab Free in 13.3.
2021-11-10 13:12:35 -05:00
> - [Improved](https://gitlab.com/gitlab-org/gitlab/-/issues/330929) in GitLab 14.5.
The GitLab Package Registry exposes the following attributes to the npm client.
These are similar to the [abbreviated metadata format ](https://github.com/npm/registry/blob/9e368cf6aaca608da5b2c378c0d53f475298b916/docs/responses/package-metadata.md#abbreviated-metadata-format ):
- `name`
- `versions`
- `name`
- `version`
- `deprecated`
- `dependencies`
- `devDependencies`
- `bundleDependencies`
- `peerDependencies`
- `bin`
- `directories`
- `dist`
- `engines`
- `_hasShrinkwrap`
2020-01-27 07:08:35 -05:00
2021-02-12 22:08:45 -05:00
## Add npm distribution tags
2020-01-27 07:08:35 -05:00
2021-11-17 16:13:37 -05:00
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/9425) in GitLab 12.8.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) from GitLab Premium to GitLab Free in 13.3.
2020-01-27 07:08:35 -05:00
2020-12-03 19:09:55 -05:00
You can add [distribution tags ](https://docs.npmjs.com/cli/dist-tag/ ) to newly-published packages.
2020-11-04 16:08:43 -05:00
Tags are optional and can be assigned to only one package at a time.
2020-01-27 07:08:35 -05:00
2020-11-04 16:08:43 -05:00
When you publish a package without a tag, the `latest` tag is added by default.
When you install a package without specifying the tag or version, the `latest` tag is used.
Examples of the supported `dist-tag` commands:
```shell
npm publish @scope/package --tag # Publish a package with new tag
npm dist-tag add @scope/package@version my-tag # Add a tag to an existing package
npm dist-tag ls @scope/package # List all tags under the package
npm dist-tag rm @scope/package@version my-tag # Delete a tag from the package
npm install @scope/package@my -tag # Install a specific tag
2020-01-27 07:08:35 -05:00
```
2020-11-04 16:08:43 -05:00
You cannot use your `CI_JOB_TOKEN` or deploy token with the `npm dist-tag` commands.
View [this issue ](https://gitlab.com/gitlab-org/gitlab/-/issues/258835 ) for details.
2021-02-12 22:08:45 -05:00
Due to a bug in npm 6.9.0, deleting distribution tags fails. Make sure your npm version is 6.9.1 or later.
2020-06-18 14:08:58 -04:00
2019-09-17 08:06:48 -04:00
## Troubleshooting
2021-03-22 14:09:24 -04:00
When troubleshooting npm issues, first run the same command with the `--verbose` flag to confirm
what registry you are hitting.
2021-07-09 14:09:51 -04:00
To improve performance, npm caches files related to a package. Note that npm doesn't remove data by
itself. The cache grows as new packages are installed. If you encounter issues, clear the cache with
this command:
```shell
npm cache clean --force
```
2021-02-12 22:08:45 -05:00
### Error running Yarn with the Package Registry for npm registry
2019-09-17 08:06:48 -04:00
2021-02-12 22:08:45 -05:00
If you are using [Yarn ](https://classic.yarnpkg.com/en/ ) with the npm registry, you may get
2019-09-17 08:06:48 -04:00
an error message like:
2020-01-30 10:09:15 -05:00
```shell
2019-09-17 08:06:48 -04:00
yarn install v1.15.2
warning package.json: No license field
info No lockfile found.
warning XXX: No license field
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
2020-11-04 16:08:43 -05:00
error An unexpected error occurred: "https://gitlab.example.com/api/v4/projects/XXX/packages/npm/XXX/XXX/-/XXX/XXX-X.X.X.tgz: Request failed \"404 Not Found\"".
2019-09-17 08:06:48 -04:00
info If you think this is a bug, please open a bug report with the information provided in "/Users/XXX/gitlab-migration/module-util/yarn-error.log".
2020-04-04 05:09:16 -04:00
info Visit https://classic.yarnpkg.com/en/docs/cli/install for documentation about this command
2019-09-17 08:06:48 -04:00
```
2020-01-27 07:08:35 -05:00
In this case, try adding this to your `.npmrc` file (and replace `<your_token>`
2020-05-14 11:08:14 -04:00
with your personal access token or deploy token):
2019-09-17 08:06:48 -04:00
2020-05-19 23:08:04 -04:00
```plaintext
2020-11-04 16:08:43 -05:00
//gitlab.example.com/api/v4/projects/:_authToken=< your_token >
2019-09-17 08:06:48 -04:00
```
2019-09-19 14:06:18 -04:00
2020-10-06 14:08:49 -04:00
You can also use `yarn config` instead of `npm config` when setting your auth-token dynamically:
```shell
2020-11-04 16:08:43 -05:00
yarn config set '//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken' "< your_token > "
yarn config set '//gitlab.example.com/api/v4/packages/npm/:_authToken' "< your_token > "
2020-10-06 14:08:49 -04:00
```
2021-02-12 22:08:45 -05:00
### `npm publish` targets default npm registry (`registry.npmjs.org`)
2019-09-19 14:06:18 -04:00
Ensure that your package scope is set consistently in your `package.json` and `.npmrc` files.
For example, if your project name in GitLab is `foo/my-package` , then your `package.json` file
should look like:
```json
{
"name": "@foo/my-package",
"version": "1.0.0",
2021-02-12 22:08:45 -05:00
"description": "Example package for GitLab npm registry",
2019-09-19 14:06:18 -04:00
}
```
And the `.npmrc` file should look like:
```ini
2020-11-04 16:08:43 -05:00
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken=< your_token >
//gitlab.example.com/api/v4/packages/npm/:_authToken=< your_token >
@foo:registry =https://gitlab.example.com/api/v4/packages/npm/
2019-09-19 14:06:18 -04:00
```
2019-12-10 02:53:40 -05:00
2021-02-12 22:08:45 -05:00
### `npm install` returns `Error: Failed to replace env in config: ${npm_TOKEN}`
2020-01-14 16:07:45 -05:00
2021-06-28 11:08:03 -04:00
You do not need a token to run `npm install` unless your project is private. The token is only required to publish. If the `.npmrc` file was checked in with a reference to `$npm_TOKEN` , you can remove it. If you prefer to leave the reference in, you must set a value prior to running `npm install` or set the value by using [GitLab CI/CD variables ](../../../ci/variables/index.md ):
2020-01-14 16:07:45 -05:00
2020-01-30 10:09:15 -05:00
```shell
2020-01-14 16:07:45 -05:00
NPM_TOKEN=< your_token > npm install
```
2020-04-27 11:10:16 -04:00
### `npm install` returns `npm ERR! 403 Forbidden`
2020-11-04 16:08:43 -05:00
If you get this error, ensure that:
2020-09-30 14:09:52 -04:00
2021-11-02 20:13:30 -04:00
- The Package Registry is enabled in your project settings. Although the Package Registry is enabled
by default, it's possible to [disable it ](../package_registry/#disable-the-package-registry ).
2020-11-04 16:08:43 -05:00
- Your token is not expired and has appropriate permissions.
2021-01-21 19:09:25 -05:00
- A package with the same name or version doesn't already exist within the given scope.
2020-11-04 16:08:43 -05:00
- The scoped packages URL includes a trailing slash:
- Correct: `//gitlab.example.com/api/v4/packages/npm/`
- Incorrect: `//gitlab.example.com/api/v4/packages/npm`
2020-12-08 19:09:42 -05:00
### `npm publish` returns `npm ERR! 400 Bad Request`
2021-06-30 14:07:05 -04:00
If you get this error, one of the following problems could be causing it.
#### Package name does not meet the naming convention
2021-07-01 14:07:29 -04:00
Your package name may not meet the
2021-06-04 11:10:25 -04:00
[`@scope/package-name` package naming convention ](#package-naming-convention ).
2020-12-08 19:09:42 -05:00
Ensure the name meets the convention exactly, including the case.
Then try to publish again.
2020-12-17 04:10:19 -05:00
2021-06-30 14:07:05 -04:00
#### Package already exists
2021-07-01 14:07:29 -04:00
Your package has already been published to another project in the same
2021-06-30 14:07:05 -04:00
root namespace and therefore cannot be published again using the same name.
2021-07-01 14:07:29 -04:00
This is also true even if the prior published package shares the same name,
2021-06-30 14:07:05 -04:00
but not the version.
2021-11-10 13:12:35 -05:00
#### Package JSON file is too large
Make sure that your `package.json` file does not [exceed `20,000` characters ](#packagejson-limitations ).
2020-12-17 04:10:19 -05:00
### `npm publish` returns `npm ERR! 500 Internal Server Error - PUT`
This is a [known issue ](https://gitlab.com/gitlab-org/gitlab/-/issues/238950 ) in GitLab
13.3.x and later. The error in the logs will appear as:
```plaintext
>NoMethodError - undefined method `preferred_language' for #< Rack::Response
```
This might be accompanied by another error:
```plaintext
>Errno::EACCES","exception.message":"Permission denied
```
This is usually a permissions issue with either:
- `'packages_storage_path'` default `/var/opt/gitlab/gitlab-rails/shared/packages/` .
- The remote bucket if [object storage ](../../../administration/packages/#using-object-storage )
is used.
2021-04-09 17:09:22 -04:00
In the latter case, ensure the bucket exists and GitLab has write access to it.
## Supported CLI commands
The GitLab npm repository supports the following commands for the npm CLI (`npm`) and yarn CLI
(`yarn`):
- `npm install` : Install npm packages.
- `npm publish` : Publish an npm package to the registry.
- `npm dist-tag add` : Add a dist-tag to an npm package.
- `npm dist-tag ls` : List dist-tags for a package.
- `npm dist-tag rm` : Delete a dist-tag.
- `npm ci` : Install npm packages directly from your `package-lock.json` file.
- `npm view` : Show package metadata.
- `yarn add` : Install an npm package.
- `yarn update` : Update your dependencies.