2020-06-23 00:09:42 +05:30
---
stage: Package
group: Package
2021-02-22 17:27:13 +05:30
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-06-23 00:09:42 +05:30
---
2021-01-29 00:20:46 +05:30
# NPM packages in the Package Registry
2019-12-04 20:38:33 +05:30
2020-10-24 23:57:45 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/5934) in [GitLab Premium](https://about.gitlab.com/pricing/) 11.7.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
Publish NPM packages in your project's Package Registry. Then install the
packages whenever you need to use them as a dependency.
2019-12-04 20:38:33 +05:30
2021-02-22 17:27:13 +05:30
Only [scoped ](https://docs.npmjs.com/misc/scope/ ) packages are supported.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
## Build an NPM package
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
This section covers how to install NPM or Yarn and build a package for your
JavaScript project.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
If you already use NPM and know how to build your own packages, go to
the [next section ](#authenticate-to-the-package-registry ).
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
### Install NPM
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
Install Node.js and NPM in your local development environment by following
2021-02-22 17:27:13 +05:30
the instructions at [npmjs.com ](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm/ ).
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
When installation is complete, verify you can use NPM in your terminal by
2020-03-13 15:44:24 +05:30
running:
```shell
npm --version
2020-01-01 13:55:28 +05:30
```
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
The NPM version is shown in the output:
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
```plaintext
2020-03-13 15:44:24 +05:30
6.10.3
2020-01-01 13:55:28 +05:30
```
2021-01-29 00:20:46 +05:30
### Install Yarn
2020-01-01 13:55:28 +05:30
2021-01-29 00:20:46 +05:30
As an alternative to NPM, you can install Yarn in your local environment by following the
instructions at [yarnpkg.com ](https://classic.yarnpkg.com/en/docs/install ).
2020-01-01 13:55:28 +05:30
2021-01-29 00:20:46 +05:30
When installation is complete, verify you can use Yarn in your terminal by
running:
2019-12-26 22:10:19 +05:30
2020-03-13 15:44:24 +05:30
```shell
yarn --version
```
2021-01-29 00:20:46 +05:30
The Yarn version is shown in the output:
2020-03-13 15:44:24 +05:30
2020-04-08 14:13:33 +05:30
```plaintext
2020-03-13 15:44:24 +05:30
1.19.1
```
2021-01-29 00:20:46 +05:30
### Create a project
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
To create a project:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
1. Create an empty directory.
1. Go to the directory and initialize an empty package by running:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
```shell
npm init
```
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
Or if you're using Yarn:
```shell
yarn init
```
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.
A `package.json` file is created.
## Use the GitLab endpoint for NPM packages
To use the GitLab endpoint for NPM packages, choose an option:
- **Project-level**: Use when you have few NPM packages and they are not in
the same GitLab group.
- **Instance-level**: Use when you have many NPM packages in different
GitLab groups or in their own namespace. Be sure to comply with the [package naming convention ](#package-naming-convention ).
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
Some features such as [publishing ](#publish-an-npm-package ) a package is only available on the project-level endpoint.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
## Authenticate to the Package Registry
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
To authenticate to the Package Registry, you must use one of the following:
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
- A [personal access token ](../../../user/profile/personal_access_tokens.md )
(required for two-factor authentication (2FA)), with the scope set to `api` .
- A [deploy token ](../../project/deploy_tokens/index.md ), with the scope set to `read_package_registry` , `write_package_registry` , or both.
- It's not recommended, but you can use [OAuth tokens ](../../../api/oauth2.md#resource-owner-password-credentials-flow ).
Standard OAuth tokens cannot authenticate to the GitLab NPM Registry. You must use a personal access token with OAuth headers.
- A [CI job token ](#authenticate-with-a-ci-job-token ).
2021-02-22 17:27:13 +05:30
- Your NPM package name must be in the format of [@scope:package-name ](#package-naming-convention ). It must match exactly, including the case.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
### Authenticate with a personal access token or deploy token
2019-12-04 20:38:33 +05:30
2021-02-22 17:27:13 +05:30
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-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
#### Project-level NPM endpoint
To use the [project-level ](#use-the-gitlab-endpoint-for-npm-packages ) NPM endpoint, set your NPM configuration:
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
```shell
# Set URL for your scoped packages.
# For example package with name `@foo/bar` will use this URL for download
2021-01-29 00:20:46 +05:30
npm config set @foo:registry https://gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
# Add the token for the scoped packages URL. Replace <your_project_id>
# with the project where your package is located.
npm config set '//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken' "< your_token > "
2019-12-04 20:38:33 +05:30
```
2021-01-29 00:20:46 +05:30
- `<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.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
You should now be able to publish and install NPM packages in your project.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
If you encounter an error with [Yarn ](https://classic.yarnpkg.com/en/ ), view
[troubleshooting steps ](#troubleshooting ).
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
#### Instance-level NPM endpoint
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
To use the [instance-level ](#use-the-gitlab-endpoint-for-npm-packages ) NPM endpoint, set your NPM configuration:
2019-12-21 20:55:43 +05:30
2020-03-13 15:44:24 +05:30
```shell
2021-01-29 00:20:46 +05:30
# 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/
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
# Add the token for the scoped packages URL. This will allow you to download
# `@foo/` packages from private projects.
npm config set '//gitlab.example.com/api/v4/packages/npm/:_authToken' "< your_token > "
```
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
- `<your_token>` is your personal access token or deploy token.
- Replace `gitlab.example.com` with your domain name.
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
You should now be able to publish and install NPM packages in your project.
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
If you encounter an error with [Yarn ](https://classic.yarnpkg.com/en/ ), view
[troubleshooting steps ](#troubleshooting ).
2020-01-01 13:55:28 +05:30
2021-01-29 00:20:46 +05:30
### Authenticate with a CI job token
2019-12-26 22:10:19 +05:30
2021-01-29 00:20:46 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/9104) in GitLab Premium 12.5.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2019-12-26 22:10:19 +05:30
2021-01-29 00:20:46 +05:30
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-10-24 23:57:45 +05:30
The token inherits the permissions of the user that generates the pipeline.
2019-12-26 22:10:19 +05:30
2021-01-29 00:20:46 +05:30
#### Project-level NPM endpoint
To use the [project-level ](#use-the-gitlab-endpoint-for-npm-packages ) NPM endpoint, add a corresponding section to your `.npmrc` file:
2019-12-26 22:10:19 +05:30
```ini
2021-01-29 00:20:46 +05:30
@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}
2019-12-26 22:10:19 +05:30
```
2019-12-21 20:55:43 +05:30
2021-01-29 00:20:46 +05:30
#### Instance-level NPM endpoint
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
To use the [instance-level ](#use-the-gitlab-endpoint-for-npm-packages ) NPM endpoint, add a corresponding section to your `.npmrc` file:
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
```ini
@foo:registry =https://gitlab.example.com/api/v4/packages/npm/
//gitlab.example.com/api/v4/packages/npm/:_authToken=${CI_JOB_TOKEN}
2019-12-04 20:38:33 +05:30
```
2021-01-29 00:20:46 +05:30
#### Use variables to avoid hard-coding auth token values
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
To avoid hard-coding the `authToken` value, you may use a variable in its place:
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
```shell
2021-01-29 00:20:46 +05:30
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-12-04 20:38:33 +05:30
```
2021-01-29 00:20:46 +05:30
Then, you can run `npm publish` either locally or by using GitLab CI/CD.
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
- **Locally:** Export `NPM_TOKEN` before publishing:
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
```shell
NPM_TOKEN=< your_token > npm publish
```
2019-12-04 20:38:33 +05:30
2021-01-29 00:20:46 +05:30
- **GitLab CI/CD:** Set an `NPM_TOKEN` [variable ](../../../ci/variables/README.md )
under your project's **Settings > CI/CD > Variables** .
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
## Package naming convention
2021-01-29 00:20:46 +05:30
Your NPM package name must be in the format of `@scope:package-name` .
- The `@scope` is the root namespace of the GitLab project. It must match exactly, including the case.
- The `package-name` can be whatever you want.
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.
2020-03-13 15:44:24 +05:30
| Project | Package | Supported |
| ---------------------- | ----------------------- | --------- |
2021-01-29 00:20:46 +05:30
| `my-org/bar` | `@my-org/bar` | Yes |
| `my-org/bar/baz` | `@my-org/baz` | Yes |
| `My-org/Bar/baz` | `@My-org/Baz` | Yes |
| `my-org/bar/buz` | `@my-org/anything` | Yes |
2020-03-13 15:44:24 +05:30
| `gitlab-org/gitlab` | `@gitlab-org/gitlab` | Yes |
| `gitlab-org/gitlab` | `@foo/bar` | No |
2021-01-29 00:20:46 +05:30
In GitLab, this regex validates all package names from all package managers:
2020-03-13 15:44:24 +05:30
2020-04-22 19:07:51 +05:30
```plaintext
2020-03-13 15:44:24 +05:30
/\A\@?(([\w\-\.\+]*)\/)*([\w\-\.]+)@?(([\w\-\.\+]*)\/)*([\w\-\.]*)\z/
```
2021-01-29 00:20:46 +05:30
This regex allows almost all of the characters that NPM allows, with a few exceptions (for example, `~` is not allowed).
The regex also allows for capital letters, while NPM does not. Capital letters are needed because the scope must be
identical to the root namespace of the project.
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
2021-01-29 00:20:46 +05:30
When you update the path of a user or group, or transfer a subgroup or project,
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
the naming convention and run `npm publish` if necessary.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
## Publish an NPM package
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
Prerequisites:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
- [Authenticate ](#authenticate-to-the-package-registry ) to the Package Registry.
- Set a [project-level NPM endpoint ](#use-the-gitlab-endpoint-for-npm-packages ).
2021-02-22 17:27:13 +05:30
- Your NPM package name must be in the format of [@scope:package-name ](#package-naming-convention ). It must match exactly, including the case.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
To upload an NPM package to your project, run this command:
2020-03-13 15:44:24 +05:30
```shell
2021-01-29 00:20:46 +05:30
npm publish
2020-03-13 15:44:24 +05:30
```
2021-01-29 00:20:46 +05:30
To view the package, go to your project's **Packages & Registries** .
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
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.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
## Publish an NPM package by using CI/CD
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
Prerequisites:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
- [Authenticate ](#authenticate-to-the-package-registry ) to the Package Registry.
- Set a [project-level NPM endpoint ](#use-the-gitlab-endpoint-for-npm-packages ).
To work with NPM commands within [GitLab CI/CD ](../../../ci/README.md ), you can use
`CI_JOB_TOKEN` in place of the personal access token or deploy token in your commands.
An example `.gitlab-ci.yml` file for publishing NPM packages:
```yaml
image: node:latest
stages:
- deploy
deploy:
stage: deploy
script:
- echo "//gitlab.example.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">.npmrc
- npm publish
2020-03-13 15:44:24 +05:30
```
2021-02-22 17:27:13 +05:30
See the
[Publish NPM packages to the GitLab Package Registry using semantic-release ](../../../ci/examples/semantic-release.md )
step-by-step guide and demo project for a complete example.
2021-01-29 00:20:46 +05:30
## Publishing packages with the same name or version
You cannot publish a package if a package of the same name and version already exists.
You must delete the existing package first.
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.
## Install a package
NPM packages are commonly-installed by using the `npm` or `yarn` commands
in a JavaScript project.
1. Set the URL for scoped packages by running:
```shell
npm config set @foo:registry https://gitlab.example.com/api/v4/packages/npm/
```
Replace `@foo` with your scope.
2020-04-08 14:13:33 +05:30
2021-01-29 00:20:46 +05:30
1. Ensure [authentication ](#authenticate-to-the-package-registry ) is configured.
2020-04-08 14:13:33 +05:30
2021-01-29 00:20:46 +05:30
1. In your project, to install a package, run:
```shell
npm install @my -project-scope/my-package
```
Or if you're using Yarn:
```shell
yarn add @my -project-scope/my-package
```
In [GitLab 12.9 and later ](https://gitlab.com/gitlab-org/gitlab/-/issues/55344 ),
when an NPM package is not found in the Package Registry, the request is forwarded to [npmjs.com ](https://www.npmjs.com/ ).
2020-04-08 14:13:33 +05:30
Administrators can disable this behavior in the [Continuous Integration settings ](../../admin_area/settings/continuous_integration.md ).
2021-01-29 00:20:46 +05:30
### Install NPM packages from other organizations
2021-01-03 14:25:43 +05:30
You can route package requests to organizations and users outside of GitLab.
2021-01-29 00:20:46 +05:30
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.
2021-01-03 14:25:43 +05:30
```shell
@foo:registry =https://gitlab.example.com/api/v4/packages/npm/
2021-01-29 00:20:46 +05:30
//gitlab.example.com/api/v4/packages/npm/:_authToken= "< your_token > "
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken= "< your_token > "
2021-01-03 14:25:43 +05:30
@my -other-org:registry=https://gitlab.example.com/api/v4/packages/npm/
2021-01-29 00:20:46 +05:30
//gitlab.example.com/api/v4/packages/npm/:_authToken= "< your_token > "
//gitlab.example.com/api/v4/projects/< your_project_id > /packages/npm/:_authToken= "< your_token > "
2021-01-03 14:25:43 +05:30
```
2021-01-29 00:20:46 +05:30
### NPM dependencies metadata
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/11867) in GitLab Premium 12.6.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
In GitLab 12.6 and later, packages published to the Package Registry expose the following attributes to the NPM client:
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
- name
- version
- dist-tags
- dependencies
- dependencies
- devDependencies
- bundleDependencies
- peerDependencies
- deprecated
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
## Add NPM distribution tags
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/9425) in GitLab Premium 12.8.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Core in 13.3.
2020-03-13 15:44:24 +05:30
2021-02-22 17:27:13 +05:30
You can add [distribution tags ](https://docs.npmjs.com/cli/dist-tag/ ) to newly-published packages.
2021-01-29 00:20:46 +05:30
Tags are optional and can be assigned to only one package at a time.
2020-03-13 15:44:24 +05:30
2021-01-29 00:20:46 +05:30
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-03-13 15:44:24 +05:30
```
2021-01-29 00:20:46 +05:30
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.
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-07-28 23:09:34 +05:30
2019-12-04 20:38:33 +05:30
## Troubleshooting
2020-11-24 15:15:51 +05:30
### Error running Yarn with NPM registry
2019-12-04 20:38:33 +05:30
2020-11-24 15:15:51 +05:30
If you are using [Yarn ](https://classic.yarnpkg.com/en/ ) with the NPM registry, you may get
2019-12-04 20:38:33 +05:30
an error message like:
2020-03-13 15:44:24 +05:30
```shell
2019-12-04 20:38:33 +05:30
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...
2021-01-29 00:20:46 +05:30
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-12-04 20:38:33 +05:30
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-22 19:07:51 +05:30
info Visit https://classic.yarnpkg.com/en/docs/cli/install for documentation about this command
2019-12-04 20:38:33 +05:30
```
2020-03-13 15:44:24 +05:30
In this case, try adding this to your `.npmrc` file (and replace `<your_token>`
2020-05-24 23:13:21 +05:30
with your personal access token or deploy token):
2019-12-04 20:38:33 +05:30
2020-05-24 23:13:21 +05:30
```plaintext
2021-01-29 00:20:46 +05:30
//gitlab.example.com/api/v4/projects/:_authToken=< your_token >
2019-12-04 20:38:33 +05:30
```
2019-12-21 20:55:43 +05:30
2021-01-03 14:25:43 +05:30
You can also use `yarn config` instead of `npm config` when setting your auth-token dynamically:
```shell
2021-01-29 00:20:46 +05:30
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 > "
2021-01-03 14:25:43 +05:30
```
2019-12-21 20:55:43 +05:30
### `npm publish` targets default NPM registry (`registry.npmjs.org`)
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",
"description": "Example package for GitLab NPM registry",
}
```
And the `.npmrc` file should look like:
```ini
2021-01-29 00:20:46 +05:30
//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-12-21 20:55:43 +05:30
```
2020-01-01 13:55:28 +05:30
2020-03-13 15:44:24 +05:30
### `npm install` returns `Error: Failed to replace env in config: ${NPM_TOKEN}`
2021-01-29 00:20:46 +05:30
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 environment variables ](../../../ci/variables/README.md ):
2020-03-13 15:44:24 +05:30
```shell
NPM_TOKEN=< your_token > npm install
```
2020-05-24 23:13:21 +05:30
### `npm install` returns `npm ERR! 403 Forbidden`
2021-01-29 00:20:46 +05:30
If you get this error, ensure that:
2021-01-03 14:25:43 +05:30
2021-01-29 00:20:46 +05:30
- Your token is not expired and has appropriate permissions.
- [Your token does not begin with `-` ](https://gitlab.com/gitlab-org/gitlab/-/issues/235473 ).
- A package with the same name doesn't already exist within the given scope.
- The scoped packages URL includes a trailing slash:
- Correct: `//gitlab.example.com/api/v4/packages/npm/`
- Incorrect: `//gitlab.example.com/api/v4/packages/npm`
2021-02-22 17:27:13 +05:30
### `npm publish` returns `npm ERR! 400 Bad Request`
If you get this error, your package name may not meet the
[@scope:package-name package naming convention ](#package-naming-convention ).
Ensure the name meets the convention exactly, including the case.
Then try to publish again.