Featured image of post How to Setup GitLab Private NPM Registry? Best Practices for Multi-Package and Permission Management

How to Setup GitLab Private NPM Registry? Best Practices for Multi-Package and Permission Management

Solve GitLab Private NPM Registry configuration challenges, including .npmrc logic, multi-package management, and 404 error troubleshooting, with best practices for a unified Registry.

Are you stuck on .npmrc configuration because your company project needs to use Private Packages?

Why does it still show 404 Not Found even though the Token is set?

When a company has multiple teams and multiple packages (e.g., @frontend, @backend), how can you elegantly manage .npmrc?

Demystifying the Two Myths of .npmrc

To understand .npmrc, you first need to understand how it works.

Simply put, its core logic boils down to two things: Scope Mapping and Authentication.

1. Scope Mapping

This tells npm: When you see a package starting with @company, don’t look for it on the official npmjs.org, but go to our specified GitLab Registry.

@company:registry=https://gitlab.com/api/v4/groups/100/-/packages/npm/

2. Authentication

With the address, you also need a key. The key is your AuthToken.

Here is a super critical rule: Registry URL and AuthToken path must “match exactly”.

# ✅ Correct: Path is exactly the same as the registry defined above
//gitlab.com/api/v4/groups/100/-/packages/npm/:_authToken=${GITLAB_TOKEN}

# ❌ Incorrect: Path does not match, which will cause permission failure
//gitlab.com/api/v4/:_authToken=${GITLAB_TOKEN}

Many people fail to configure it often because of an extra slash or a missing level in the path.

Dependency Hell and 404 Errors

The most headache-inducing situation is: You install package A, which depends on package B (also private), and then npm throws a 404 Not Found.

Why does this happen? That’s because npm gets lost when resolving dependencies if there is no correct correspondence.

Especially when you have two different Scopes (e.g., @team-a and @team-b) in different GitLab Groups, .npmrc can become extremely complicated and difficult to maintain. Coupled with the fact that different Registries may require different Tokens, managing these environment variables alone is enough to fill you up.

Is there a smarter way?

Registry Description
Group Registry To be able to unify the download of all packages, unify the setting of Group Registry to download all packages under this GitLab Group.
Project Registry To have permissions when publishing, each project can set its own Project Registry to publish.

Why do this?

Reason Description
Simple Installation Can easily install and download all packages in the same group.
Simple Publishing Can easily publish packages to their respective projects, avoiding different packages being published to the same Registry, each project has its own Registry.
Easy Token Management Whether on a developer’s computer or in a CI/CD environment, only one set of GITLAB_NPM_TOKEN needs to be maintained.
Avoid Dependency Errors All private packages are in the same place, npm will absolutely not fail to find them.

Configuration in Action

You can directly copy this template:

Remember to hand over the work of protecting Tokens to environment variables, never hardcode Tokens directly in the code!

# .npmrc
# General domain-level auth token (covers all group and project level installation requests)
//gitlab.com/:_authToken=${GITLAB_NPM_TOKEN}

# For publish: project-level auth token (npm publish requires exact path matching)
//gitlab.com/api/v4/projects/YOUR_PROJECT_ID/packages/npm/:_authToken=${GITLAB_NPM_TOKEN}

# @company scope uses group level registry
# All @company/* packages will be installed from this registry
@company:registry=https://gitlab.com/api/v4/groups/YOUR_GROUP_ID/-/packages/npm/

# Other public packages still go to the official library
registry=https://registry.npmjs.org/

Configure publishConfig in package.json to publish the package to the corresponding GitLab Project Registry.

{
  "name": "@company/my-package",
  "publishConfig": {
    "@company:registry": "https://gitlab.com/api/v4/projects/YOUR_PROJECT_ID/packages/npm/"
  }
}

Conclusion

Through the strategy of “Unified Download Group Registry” and “Independent Publish Project Registry”, we can significantly reduce maintenance costs and make the development process smoother.

Goal Description
Precise Matching Scope mapping and Token path must be completely consistent
Safety First Make good use of environment variable ${GITLAB_NPM_TOKEN} to manage private information
Centralized Management Prioritize using a unified GitLab Group to download all company private packages
Independent Publishing Prioritize using independent GitLab Projects to publish respective private packages, managing respective packages independently to avoid confusion

mastering these logics, GitLab NPM Registry will be a powerful helper in your company’s development process!

All rights reserved,未經允許不得隨意轉載
Built with Hugo
Theme Stack designed by Jimmy