doc/development/npmjs.md
GitLab uses npm packages as a means to improve code reuse and modularity in and across projects. This document outlines the best practices and guidelines for securely publishing npm packages to npmjs.com.
By adhering to these guidelines, we can ensure secure and reliable publishing of NPM packages, fostering trust and consistency in the GitLab ecosystem.
npm info <yourpackage> alias to verify what a given alias points to.
Ensure that you're confident that all aliases point to legitimate packages that you trust..npmrc, and .env.gitlab-bot as author of the package. This ensures the organization retains ownership if a team member's email becomes invalid during offboarding.package-lock.json or yarn.lock) to ensure consistency in dependencies across environments.npm ci (or yarn install --frozen-lockfile) instead of npm install in CI/CD pipelines
to ensure dependencies are installed exactly as defined in the lock file.Packages must only be published through GitLab CI/CD pipelines on a protected branch, not from local developer machines. This ensures:
To set up publishing through GitLab CI/CD:
.gitlab-ci.yml for publishing the package. An example is provided below@organization-name/package-name) to prevent namespace pollution or name-squatting by other users.package.json file:
files in package.json to explicitly include only necessary files in the published package.publishConfig.access: 'restricted'.Below is an example .gitlab-ci.yml configuration for publishing an NPM package. This codeblock isn't meant to be used as-is and will require changes depending on your configuration. This means that you will need to modify the example below to include the location of your npmjs publishing token.
stages:
- test
- build
- deploy
test:
stage: test
image: node:22
script:
- npm ci
- npm test
build:
stage: build
image: node:22
script:
- npm ci
- npm run build
publish:
stage: deploy
image: node:22
script:
- npm ci
- npm run build
- npm publish
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
README.md to ensure clear communication with users.unique-package: Generic package not specific to GitLab.existing-package-gitlab: A forked package with GitLab-specific modifications.@gitlab/specific-package: A package developed for internal GitLab use.Manual workflows should be avoided to ensure that: