Automatically publish NPM packages with Travis CI

Travis CI allows you to automate the test, build and publish of npm packages or any other type of project.
The great thing about Travis CI is that it’s FREE to use for open source projects.

On the end of this post you should be able to let Travis build and publish a npm package automatically after pushing
to the releasebranch.

Advantages

  • By setting up a Travis CI pipeline you can perform automated tests before the actual release which helps with the quality of the package.
  • Once setup, everything can be automated, from testing to building and publishing. You only need to write code and push it to a git repository.
  • It’s FREEfor open source projects

Disadvantages

  • GitHub repository needs to be public if you want to use Travis CI for free.

Requirements

  1. You need a GitHubaccount
  2. You need a TravisCIaccount (you connect with your GitHub account)
  3. You need a NPMaccount
  4. You need a publicgit repository which contains the source code of your project.

 

Setting up Travis account

  1. Once registered, go to the Travis CI dashboardand enable Travis CI for your public GitHub project
  2. Create a new token for npm, use access level ‘Read and Publish’ (can be found in the profile settings page on https://www.npmjs.com)

We are going to use that generated token and set it as environment variable inside the Travis dashboard. Go to the settings of your project inside Travis and search for the environment variables section.

  1. Set 'NPM_API_TOKEN'to the generated token from npm
  2. Set 'NPM_EMAIL'to the email address which you registered on npm

 

By setting the NPM_API_TOKENand NPM_EMAILwe can allow Travis to publish packages on your behalf.

Setting up build definition file

In the root of your project you need a file called .travis.yml.This file defines the build and deploy process.
Travis CI will automatically read that file on each commit.

We can define everything we need from bottom to top:

  • 'deploy'will run only on commits to the ‘release’ branch and will automatically replace 'NPM_API_TOKEN'
    and 'NPM_EMAIL'with the correct values from the environment variables which we defined earlier in the Travis CI dashboard.
  • 'before_deploy'will only run on commits to the releasebranch. In this case, the project will be build using npm run build.
    If the npm command fails, the deploy to npm will not happen.
  • 'script'will always run on every commit. In this case, unit tests and linting will run.
    If one of those npm commands fails, the deploy to npm will not happen.

Publish and versioning

Before pushing to the releasebranch, be sure to update the version number of yourpackage.json
This can easily be done by using one of the following npm commands:

$ npm version patch
$ npm version minor
$ npm version major

patch: 1.0.0 -> 1.0.1
minor:  1.0.0 -> 1.1.0
major:  1.0.0 -> 2.0.0

By using the npm version command, npm will automatically create and commit a git tag for you.
When you push your code to the releasebranch, Travis CI will automatically build, test and publish to npm for you!

As you can see it’s pretty easy to get started with Travis CI and creating a pipeline for building, testing and publishing npm packages.

Lars Kniep

Lars Kniep is Senior Software Engineer en werkt sinds 2017 als full-stack developer bij Profit4Cloud. Lars is AWS-CSA/A gecertificeerd.