ARTIFACTORY: Configure an NPM Gitlab registry in remote repository

AuthorFullName__c
Shisiya Sebastian
articleNumber
000005561
FirstPublishedDate
2023-01-29T09:23:43Z
lastModifiedDate
2025-07-22

ARTIFACTORY: Configure an NPM Gitlab registry in remote repository

Here, we are going to see how to configure a Gitlab private NPM Registry in the Artifactory remote repository. If you do not already have a GitLab NPM registry, please scroll down to find the steps to prepare Gitlab NPM registry.

Create a Gitlab Remote repository

The steps to mirror the Gitlab private repository in JFrog Artifactory.
Step 1: Create a Deploy token from Gitlab and copy the Username and Access token
Step 2: Create an NPM remote repository in Artifactory with the Gitlab URL


1. Go to Artifactory → Administration → Repositories → Add Repositories → Remote Repositories → NPM
2. Create a repository with <your_npm_reponame> as the Repository Key
3. Update URL field with the Gitlab registry URL in the below format
https://gitlab.com/api/v4/projects/<project_ID>/packages/npm/
4. Add the Deploy Token username and Access token
5. Create Remote Repository


Here is an example of a remote npm repository with the name “npmlab”

User-added image


Note: Since it is a private registry, “Test” may show “Target remote URL returned error 404: Not Found”, however you can ignore this error message and move forward. 

Step 3: Copy the NPM client configuration snippets from “Set Me Up” and update .npmrc


1. Go to Artifactory → Application → Artifacts → your_npm_reponame → Set Me Up(Top Right Hand Side)
2. Enter password in the respective text box and copy the code snippet for scoped packages under Using basic authentication
3. Replace <SCOPE> with the scope in your registry

User-added image

4. Add the code snippet to ~/.npmrc file(If Windows, %USERPROFILE%\.npmrc)
 

User-added image


Step 4: Install npm package using “npm install @<SCOPE>/package_name”
 

User-added image
To Prepare GitLab NPM Registry
Create a Project in GitLab:


GitLab > Create new project > Create blank project > Select Project deployment target as “Registry”

User-added image


For more details, please refer to this documentation.

To publish an NPM package, the following is required:


1. Project ID: Click on your project and you can find your Project ID

User-added image

2. Scope: The value used for the @scope is the root of the project that will host the packages and not the root of the project with the source code of the package itself. The scope should be lowercase.

3. Deploy Token
Create a Deploy Token:
  • 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

User-added image
User-added image




Create a sample npm package and publish it to GitLab:

1. npm init 
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


User-added image


Ref: https://docs.gitlab.com/ee/user/packages/npm_registry/