Create multiple npm packages with gitlab's Package Registry

I’ve been trying to create several npm packages with the gitlab Package Registry. I followed this documentation npm packages in the Package Registry | GitLab about it.

Describe your question in as much detail as possible:
I’d like to create 2 npm packages to use them in other private projects. I’d like to have those 2 packages in the same sub group such as:

mainGroup
|- subGroup
   |- project1
   |- project2

Plus, I’d like to name them @mainGroup/project1 and @mainGroup/project2

I manage to create the package registry by building the projects and adding the following config to the .npmrc file:

@mainGroup:registry=https://gitlab.my-gitlab.fr/api/v4/projects/<project_id>/packages/npm/

//gitlab.my-gitlab.fr/api/v4/projects/<project_id>/packages/npm/:_authToken=<auth_token>

Then with the publish command I finally had both packages

This is where it gets tricky:

On a third project I created the .npmrc file with :

@mainGroup:registry=https://gitlab.my-gitlab.fr/api/v4/projects/<project1_id>/packages/npm/

//gitlab.my-gitlab.fr/api/v4/projects/<project1_id>/packages/npm/:_authToken=<auth_token>

Then I added the first package with yarn add @mainGroup/project1 : it worked fine

But I can’t add the @mainGroup/project2 package because of the conflict in the .npmrc file

I believe using the instance-level endpoint instead of the project one could be an option, but I’m really having trouble using it… Could someone please help me?

1 Like

If your projects are public, you can use public gitlab instance-level registry.
So, you need your {project_id} (you can find it on project home page) and {your_org_name} (from URL: https://gitlab.com/{your_org_name}/{your_project_name}/)

The steps for the nodejs project:

How to create new instance-level package

  • package.json file: make sure your package name looks like
"name": "@{your_org_name}/{your_package_name}",
  • package.json file: add section
"publishConfig": {
    "registry": "https://gitlab.com/api/v4/projects/{your_project_id}/packages/npm/"
}
  • (optional) .npmrc file: create or update content (ensure file has new line at the end) if you want to use other packages from public registry
echo @{your_org_name}:registry=https://gitlab.com/api/v4/packages/npm/ >> .npmrc
  • .gitlab-ci.yml file: add publish stage to resolve registry auth by GitLab CI
image: node:latest

stages:
  - build
  - publish

build:
  stage: build
  artifacts:
    expire_in: 1 week
    paths:
      - dist
  script:
    - npm install
    - npm run build
  only:
    refs:
      - main

publish:
  stage: publish
  script:
    - echo "@{your_org_name}:registry=https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/packages/npm/">>.npmrc
    - echo "//${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/packages/npm/:_authToken=${CI_JOB_TOKEN}">>.npmrc
    - npm publish
  only:
    refs:
      - main
  • now you will publish new versions on commits to main branch of repository and you can use other packages from the same scope in public registry

How to use all instance-level packages in external projects

echo @{your_org_name}:registry=https://gitlab.com/api/v4/packages/npm/ >> .npmrc
npm install
npm install @{your_org_name}/{your_package_name}