Using NPM Private Modules in Your CI Workflow
Sometimes we need to make use of NPM private modules when running our CI workflows. In this post, we take a look at how to do just that quickly and easily!
Join the DZone community and get the full member experience.
Join For FreeIn 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 YAML, 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 bBuild Machine
Last, 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!
Published at DZone with permission of Manisha Sahasrabudhe, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Leveraging FastAPI for Building Secure and High-Performance Banking APIs
-
GitLab Pages Preview
-
Overcoming Challenges in UI/UX Application Modernization
-
How to Format Articles for DZone
Comments