Using NPM Private Modules In Your CI Workflow

- By Manisha Sahasrabudhe on September 19, 2017

In addition to publicly available packages, npm also allows users to publish and manage packages in a private namespace. If you want your npm install command in the CI workflow to install your private dependencies, there are a couple of setup steps that are required.

Unless you follow these steps, your npm install command will throw an error, such as 

  npm ERR! 404 Not found : @manishas/my-private-module   

 

Step 1: Get your npm auth token

First, you need to find your npm token, which is stored in the .npmrc file in your home directory. The file will contain a line which looks like this:

  //registry.npmjs.org/:_authToken=GFYR$E8D-89DG-GH54-HJDR-TYDGOSYT785T  

Copy the value of the token, which is everything after authToken=

 

Step 2: Add the npm token to your build environment

Next, you need to set an environment variable $NPM_TOKEN in your CI workflow. Ordinarily, you would just set this in the env section of your yml like this:

env:
  - NPM_TOKEN=GFYR$E8D-89DG-GH54-HJDR-TYDGOSYT785T

However, this will expose your npm token to everyone who has access to your shippable.yml, which means everyone who has access to your repository. To avoid disclosing the token in your config, you can use secure variables. To do this, go to your Shippable UI, navigate to the Subscription or Project Settings page, and enter the following in the Encrypt section:

NPM_TOKEN=GFYR$E8D-89DG-GH54-HJDR-TYDGOSYT785T

You will get your encrypted string which should be include in shippable.yml:

env:
  - secure: <encrypted string>

This will add the environment variable NPM_TOKEN to the build environment. Please note that if you change your npm password, then you'll need to update your NPM_TOKEN.

 

Step 3:  Add the .npmrc file to your build machine

Lastly, your build machine needs the .npmrc file. The easiest thing to do is to commit it to the folder in your repository from where the npm install command is executed during CI. This will ensure that the file gets copied to the build machine when the repository is cloned.

Alternatively, you can also generate the file dynamically in your CI config, before calling npm install:

ci:
- echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc
- npm install

 

And that's it! You should be all set to pull private npm packages during your CI workflow!

Topics: continuous integration (CI), nodejs, npm