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 release branch.


  • 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 FREE for open source projects


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


  1. You need a GitHub account
  2. You need a TravisCI account (you connect with your GitHub account)
  3. You need a NPM account
  4. You need a public git repository which contains the source code of your project.


Setting up Travis account

  1. Once registered, go to the Travis CI dashboard and 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

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_TOKEN and NPM_EMAIL we 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 release branch. 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 release branch, be sure to update the version number of your package.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 release branch, 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.