NPM
Common commands
bash
npm install <package> --save|--global
npm uninstall <package>GitLab NPM Registry
Authenticate
Personal access token
- Go to the GitLab user account page and open Access Tokens.
- Add a new token with
apiaccess and save it. - Copy and store the token safely.
- Set the authentication token.
sh
npm config set -- "//gitlab.example.com/api/v4/packages/npm/:_authToken" "<token>"Scope
The scope is usually the root namespace in GitLab.
For example: https://gitlab.com/jckoh/software-v2/lib/logger
The scope is jckoh.
Library development
- Create an empty directory.
- Initialize the npm project and fill in the required information. For the package name, follow the GitLab documentation: Package Naming Convention
sh
npm init- Create
.npmrcin the project directory. - Configure the npm registry.
sh
npm config set @<scope>:registry https://gitlab.com/api/v4/projects/<project_id>/packages/npm/
npm config set -- '//gitlab.com/api/v4/projects/<project_id>/packages/npm/:_authToken' "<token>"- Publish the package.
sh
npm publish- You can also define the publish config in
package.json.
json
{
"publishConfig": { "@<scope>:registry":" https://gitlab.com/api/v4/projects/<project_id>/packages/npm/" }
}Using private npm packages
- Set the scope.
sh
npm config set @<scope>:registry https://gitlab.com/api/v4/packages/npm/- Install the npm package.
sh
npm install @scope/packagePublish to the public npmjs.com registry
- Init:
sh
npm init --scope=@scope-name- Publish:
sh
npm publish --access publicpnpm
Workspace
Create pnpm-workspace.yaml in the root folder:
yaml
packages:
- apps/*
- packages/*
- test/*To install a library to an app:
bash
pnpm add @company/lib --filter ./ --workspaceLink library
txt
pnpm add file:<path to library>
# At library you want to share
pnpm link
# At project want to use library
pnpm link <pkg>Security
Minimum Release Age
ini
; per project (./.npmrc) or global (~/.npmrc)
min-release-age=7yaml
# per project (./pnpm-workspace.yaml) or global (~/.config/pnpm/config.yaml)
minimumReleaseAge: 10080toml
# per project (./bunfig.toml) or global ($HOME/.bunfig.toml) or ($XDG_CONFIG_HOME/.bunfig.toml)
[install]
minimumReleaseAge = 604800Disable install scripts
ini
# .npmrc
ignore-scripts=trueBlock git dependencies
ini
# .npmrc
allow-git=none
# or
allow-git=rootyaml
# pnpm-workspace.yaml
blockExoticSubdeps: true
trustPolicy: no-downgrade