Brian Veltman

Journey of a DevOps Engineer

Working with multiple npm registries in a project

NPM

Recently I was working on a project that depended on multiple npm registries.
I quickly ended up with a broken build since all packages are fetched from the public npm registry by default (http://registry.npmjs.org/) and thus a 404 error was thrown for the private and other packages.

Scopes to the rescue!

First, we have to tell npm where to find our private registry. Since every project can have it’s own private registry it’s a good practice to create a .npmrc file inside your project.
We tell npm to use the http://private.acme.com registry for all packages with the @private scope. You can name the scope to whatever you like, but for this example we use @private.

[code lang=text]
// .npmrc
@private:registry=http://private.acme.com/
[/code]

Now we have to tell package.json which packages we’d like to fetch from the private registry. This can be done by prefixing the scope, in our case @private.

[code lang=text]
// package.json
“dependencies”: {
“concurrently”: “^3.3.0”,
“react”: “^15.4.0”,
“react-dom”: “^15.4.0”,
“@private/some-package”: “^1.0.0”
}
[/code]

Most likely you need to be authenticated to fetch from the private registry.
You can either specify the credentials in your .npmrc file or login using the terminal running the following command: npm login --registry=http://private.acme.com --scope=@private

Hopefully this helped you out making your dependencies more flexible.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.