Private NPM modules + Travis

How to create private npm modules and configure Travis to use them

Ronny Roeller
NEXT Engineering
2 min readJan 27, 2017

--

We love npm to manage our JavaScript dependencies. And, we love Travis to continuously build and test our application. This post explains how to create private npm modules and how to configure Travis to use them.

Creating a private NPM module

Creating a private npm module is simple. Just add a scope to your name. For example, add the following to your package.json to make the module fancy-formatter private to your company:

{
"name": "@my-company/fancy-formatter",
"version": "1.0.0",
...

Next, you have to let npm know who you are:

$ npm login
Username: your_username
Password: your_password
Email: (this IS public) your@email.com

Now, you can publish your module to the npm repository — which will make it automatically private:

$ npm publish

PS: Private repositories require a paid npmjs account. This page is a good starting point if you want to learn more about npm private modules.

Using private NPM modules locally

Adding the private module to your application works the same as adding any other module:

npm install @my-company/fancy-formatter --save

Whoever wants to build the project: npm will have to know that the person is allowed to access the private repository. Hence, you need to instruct all your colleagues to login via npm login before they build the next time.

Configure Travis to work with a private NPM module

So, now we have a project that depends on a private module. Let’s get Travis to build it!

First, we need to get our npm token from .npmrc (here is a great explanation on the details). Next, we store the npm token into our Travis config file with the following command line tool:

$ travis encrypt NPM_TOKEN=00000–1111–2222–3333–3278234543 --add

Finally, we adjust our .travis.yml to always add the token before installing the npm dependencies:

before_install:
- npm config set //registry.npmjs.org/:_authToken=$NPM_TOKEN

Happy coding!

Want to learn more about coding? Have a look to our other articles.

--

--

Ronny Roeller
NEXT Engineering

CTO at nextapp.co # Product discovery platform for high performing teams that bring their customers into every decision