Open In App

How to Publish NPM Packages in the Package Registry ?

Last Updated : 02 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The npm Package Registry serves as an invaluable tool for publishing npm packages for individual developers or organizations.

This article will guide you through the requirements, configuration, and usage of the npm package registry.

Setting Up the Package Registry

To utilize the package registry, you first need to configure a new package source. This can be accomplished with the following commands:

npm config set "//gitea.example.com/api/packages/{owner}/npm/:_authToken" "{token}"

The parameters used in the command are defined as follows:

Parameter

Description

scope

The scope of the packages.

owner

The owner of the package.

token

Your personal access token.

For instance, if you want to set the registry for a scope @sample and an owner testuser, you would use:

npm config set @sample:registry https://gitea.example.com/api/packages/testuser/npm/

npm config set “//gitea.example.com/api/packages/testuser/npm/:_authToken” “personal_access_token”

Publishing a Package

You can publish a package by running the npm publish command in your project directory. Note that you cannot publish a package if a package of the same name and version already exists. In such a case, you would need to delete the existing package first.

Authenticating via the .npmrc

The.npmrc file should be created or edited in the same directory as your package.json. Add the subsequent lines to the.npmrc file:

  • To publish the package to the root level group of the project you are publishing to, replace @scope.
  • Put your domain name in place of your_domain_name; for example, gitlab.com.
  • Your project ID, which can be found on the project overview page, should be replaced with your_project_id.
  • The token you established later on in the procedure is linked to “${NPM_TOKEN}”.

Publishing a package via the command line

Link your token to “${NPM_TOKEN}” in the.npmrc file. Use a deploy token, group access token, project access token, or personal access token in place of your token.

NPM_TOKEN=your_token npm publish

Your package should now publish to the package registry.

Publishing a package by using a CI/CD pipeline

You can authenticate with your project’s package registry when publishing via a CI/CD pipeline by utilising the predefined variables ${CI_PROJECT_ID} and ${CI_JOB_TOKEN}. We build a.npmrc file for authentication during the execution of your CI/CD task using these variables.

In the GitLab project containing your package.json, edit or create a .gitlab-ci.yml file. For example:

# Define the Docker image to be used
image: node:latest

# Define the stages of the pipeline
stages:
- deploy

# Define the deployment stage
publish-npm:
stage: deploy
script:
# Set up npm configuration for publishing packages
- echo "@myscope:registry=https://${MY_CI_SERVER_HOST}/api/v4/projects/${MY_CI_PROJECT_ID}/packages/npm/" > .npmrc
- echo "//${MY_CI_SERVER_HOST}/api/v4/projects/${MY_CI_PROJECT_ID}/packages/npm/:_authToken=${MY_CI_JOB_TOKEN}" >> .npmrc
# Publish the package to the npm registry
- npm publish

Replace @scope with the scope of the package that is being published.

Your package is published to the package registry when the publish-npm job in your pipeline runs.

Unpublishing a Package

To delete a package, use the npm unpublish command followed by the package name and optionally, the package version:

npm unpublish {package_name}[@{package_version}]

Example:

npm unpublish my-package@1.0.0

Installing a Package

To install a package from the package registry, use the npm install command followed by the package name:

npm install {package_name}

Example

npm install express@latest

Tagging a Package

The package registry supports version tags which can be managed by the npm dist-tag command:

npm dist-tag add {package_name}@{version} {tag}

Example:

npm dist-tag add my-package@1.0.0 latest

Note that the tag name must not be a valid version. All tag names which can be parsed as a version will be rejected.

Searching Packages

While the registry supports searching, it does not support special search qualifiers like author :gitea.

Supported Commands

The following commands are supported by the npm package registry:

npm install

This command is used to install dependencies listed in the package.json file.

  • Open your terminal or command prompt.
  • Navigate to your project directory.

Run the command:

 npm install.

npm ci

This command is similar to npm install, but it’s designed for continuous integration environments to install dependencies more quickly and more consistently.

  • Open your terminal or command prompt.
  • Navigate to your project directory.

Run the command:

npm ci

npm publish

This command is used to publish a package to the npm registry.

  • Make sure you have a package.json file in your project directory.
  • Ensure your package has a unique name that hasn’t been used before on npm.

Run the command:

 npm publish

npm unpublish

This command is used to unpublish a package from the npm registry.

  • Navigate to your package’s directory.

Run the command:

npm unpublish <package_name>@<version>

npm dist-tag

This command allows you to add, remove, or modify distribution tags for a given package.

Run the command:

npm dist-tag add <package_name>@<version> <tag> 

to add a tag to a specific version of a package.

Run the command:

npm dist-tag rm <package_name> <tag> 

to remove a tag from a package.

Run the command:

npm dist-tag ls <package_name> 

to list all tags for a package.

npm view

This command is used to view registry info about a package, version, or user.

Run the command:

npm view <package_name> 

to view information about a package.

Run the command:

npm view <package_name> 

versions to view all versions of a package.

Run the command:

npm view <package_name>

dependencies to view dependencies of a package.

npm search

This command is used to search for packages in the npm registry.

Run the command:

npm search <keyword> 

to search for packages containing the specified keyword.

Conclusion

the npm package registry serves as a vital hub for developers, offering a centralized location to store and share npm packages. This script enhances the efficiency of package publishing within your CI/CD pipeline, streamlining the process and ensuring seamless integration. By leveraging configuration settings and authentication mechanisms, it facilitates reliable package distribution to the registry. Ultimately, the accessibility and organization provided by the npm package registry empower developers to collaborate, innovate, and build robust software solutions with ease.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads