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 issues, including .npmrc logic, multi-package management, and 404 troubleshooting. Provides best practices for unified registry.

Are you trying to use Private Packages in your company project but stuck on .npmrc configuration?

Why do you still get 404 Not Found even after setting the Token?

When the company has multiple teams and multiple packages (e.g., @frontend, @backend), how do you gracefully manage .npmrc?

Breaking Down the Two Myths of .npmrc

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

Simply put, its core logic is just two things: Scope Mapping and Authentication.

1. Scope Mapping

This tells npm: When you see a package starting with @company, don’t go to the official npmjs.org, 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 crucial rule: The 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 mismatch, this will cause permission failure
//gitlab.com/api/v4/:_authToken=${GITLAB_TOKEN}

Many people fail in configuration often because the path has an extra slash or lacks a level.

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 spits out a 404 Not Found.

Why does this happen? That’s because when npm resolves dependencies, if there isn’t a correct relationship, it gets lost.

Especially when you have two different Scopes (e.g., @team-a and @team-b) in different GitLab Groups, .npmrc becomes extremely complex and hard to maintain. Plus, different Registries might need different Tokens, just managing these environment variables is exhausting.

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 For permission during publishing, each project can set its own Project Registry for publishing.

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 publishing to the same Registry; each project has its own Registry.
Easy Token Management Whether on developer’s computer or CI/CD environment, only need to maintain one set of GITLAB_NPM_TOKEN.
Avoid Dependency Errors All private packages are in the same place, npm will definitely find them.

Configuration in Action

You can directly copy this template:

Remember to hand over the task of protecting Token to environment variables, absolutely do not hardcode the Token in the code!

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

# GitLab npm registry auth config
# Use common gitlab.com auth token (covers all group and project level requests)
//gitlab.com/api/v4/groups/YOUR_GROUP_ID/-/packages/npm/:_authToken=${GITLAB_NPM_TOKEN}
# Use project registry to publish packages
//gitlab.com/api/v4/projects/YOUR_PROJECT_ID/packages/npm/:_authToken=${GITLAB_NPM_TOKEN}


# Other public packages still go to official registry
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 flow smoother.

Goal Description
Precise Match Scope mapping and Token path must match exactly
Safety First Use environment variable ${GITLAB_NPM_TOKEN} to manage private info
Centralized Management Prioritize using unified GitLab Group to download all company private packages
Independent Publish Prioritize using independent GitLab Project to publish respective private packages, managing packages independently to avoid chaos

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

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