Thumbnail image

Working With Multiple NPM Registries in One Project

!
Warning: This post is over 365 days old. The information may be out of date.

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.

1// .npmrc
2@private:registry=http://private.acme.com/

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.

1// package.json
2"dependencies": {
3    "concurrently": "^3.3.0",
4    "react": "^15.4.0",
5    "react-dom": "^15.4.0",
6    "@private/some-package": "^1.0.0"
7}

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.

Related Posts