- Go to Your Project > Settings > Repository > Deploy Tokens.
- Enter the Name, Username, and select read_package_registry, write_package_registry as Scopes
- Create deploy Token
Create a sample npm package and publish it to GitLab:
1. npm init
2. Add publishConfig with the npm package registry location.
2. Add publishConfig with the npm package registry location.
Sample Package.json:
{ "name": "@testjf/helloworld", "version": "1.0.0", "description": "To print Hello World.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [ "Hello" ], "author": "Your Name", "license": "ISC", "publishConfig": { "@testjf:registry": "https://gitlab.com/api/v4/projects/<project_ID>/packages/npm/" } }
3. Create or edit the .npmrc file in the same directory as your package.json. Include the following lines in the .npmrc file:
# Set URL for your scoped packages. @jftest:registry=https://gitlab.com/api/v4/projects/<project_ID>/packages/npm # Add token for uploading to the registry. Replace <my-project-id> //gitlab.com/api/v4/projects/<project_ID>/packages/npm/:_authToken="${NPM_TOKEN}"
Never hardcode GitLab tokens (or any tokens) directly in .npmrc files or any other files that can be committed to a repository.
4. Sample index.js file
function helloWorld() { console.log('Hello World from this npm package'); } module.exports = helloWorld;
5. Publish the package to Gitlab
NPM_TOKEN=<my_deloy_token> npm publish
Ref: https://docs.gitlab.com/ee/user/packages/npm_registry/