Below is an in-depth guide on how to set up a private npm repository, including various alternatives and practical code snippets to help you get started. Whether you’re a solo developer or part of a large team, hosting your own npm packages privately can give you control, flexibility, and improved security.
Self-Hosted Solutions
Managed by Git Hosts
npm Enterprise
Verdaccio is an open-source npm registry proxy that’s easy to set up and use. It allows you to host private packages and also cache public packages from the official npm registry.
Assuming Node.js is already installed on your machine:
# Install Verdaccio globally npm install --global verdaccio
verdaccio
By default, Verdaccio starts on port 4873. You can open your browser to http://localhost:4873 to see the Verdaccio UI.
Verdaccio creates a default config file on first run. You can customize it by editing it (the file path may vary depending on your system). A typical config (~/.config/verdaccio/config.yaml) looks like:
# Install Verdaccio globally npm install --global verdaccio
verdaccio
This prompts for username, password, and email. Once done, you’ll be logged in to your private registry.
In a package directory with a valid package.json:
storage: ./storage auth: htpasswd: file: ./htpasswd max_users: 100 uplinks: npmjs: url: https://registry.npmjs.org/ packages: '@*/*': access: $all publish: $authenticated proxy: npmjs '**': access: $all publish: $authenticated proxy: npmjs middlewares: audit: enabled: true logs: - { type: stdout, format: pretty, level: http }
That’s it! Your package is now published to your local Verdaccio registry.
To install a package from this registry, you can either:
npm adduser --registry http://localhost:4873
npm publish --registry http://localhost:4873
If you already host your code on GitHub, using GitHub Packages can be a convenient way to keep everything under one roof.
Create a Personal Access Token (PAT) with the read:packages and write:packages scopes. You can generate this token from your GitHub settings under Developer settings -> Personal access tokens.
Add your token to .npmrc:
npm install <package-name> --registry http://localhost:4873
Replace YOUR_GITHUB_USERNAME with your actual username or GitHub organization name.
Update your package.json with a scope matching your GitHub username or organization:
registry=http://localhost:4873
Then publish:
//npm.pkg.github.com/:_authToken=YOUR_PERSONAL_ACCESS_TOKEN @YOUR_GITHUB_USERNAME:registry=https://npm.pkg.github.com
Make sure .npmrc is pointing to GitHub Packages, then:
{ "name": "@YOUR_GITHUB_USERNAME/my-private-package", "version": "1.0.0", "publishConfig": { "registry": "https://npm.pkg.github.com" } }
GitLab also provides a built-in package registry.
Create or update your local/global .npmrc file with your GitLab credentials:
npm publish
Update your package.json scope to match the GitLab group or user namespace:
npm install @YOUR_GITHUB_USERNAME/my-private-package
Then publish:
# Install Verdaccio globally npm install --global verdaccio
verdaccio
If you’re looking for a robust, on-premise solution that supports multiple repository types, Sonatype Nexus or JFrog Artifactory might be your best bet.
Use a similar .npmrc setup to point your npm client to your new Nexus npm repository:
storage: ./storage auth: htpasswd: file: ./htpasswd max_users: 100 uplinks: npmjs: url: https://registry.npmjs.org/ packages: '@*/*': access: $all publish: $authenticated proxy: npmjs '**': access: $all publish: $authenticated proxy: npmjs middlewares: audit: enabled: true logs: - { type: stdout, format: pretty, level: http }
Publish your package as normal:
npm adduser --registry http://localhost:4873
npm publish --registry http://localhost:4873
Publish using:
npm install <package-name> --registry http://localhost:4873
For large organizations needing full control, auditing, and advanced security, npm Enterprise is an option. It provides:
Consult npm Enterprise documentation for setup instructions.
Setting up a private npm repository gives you the freedom to manage and host your own packages securely. Whether you’re using a self-hosted solution like Verdaccio, leveraging managed solutions like GitHub Packages or GitLab Packages, or opting for enterprise solutions like Nexus, Artifactory, or npm Enterprise—the fundamentals remain the same:
Choose the approach that best fits your organization’s requirements around security, scalability, and maintenance. With the examples and code snippets above, you should have a solid foundation to get started hosting your own private npm packages. Happy coding!
The above is the detailed content of Private npm Repositories. For more information, please follow other related articles on the PHP Chinese website!