Building a JavaScript and Node.js project

What This Guide Covers

Language versions and other build-environment specific information are in our reference pages:

This guide covers build environment and configuration topics specific to JavaScript and Node.js projects. Please make sure to read our Getting Started and general build configuration guides first.

Specifying Node.js versions

The easiest way to specify Node.js versions is to use one or more of the latest releases in your .travis.yml:

  • node latest stable Node.js release
  • iojs latest stable io.js release
  • lts/* latest LTS Node.js release
  • 8 latest 8.x release
  • 7 latest 7.x release
  • 6 latest 6.x release
  • 5 latest 5.x release
  • 4 latest 4.x release
language: node_js
  - "iojs"
  - "7"

More specific information on what versions of Node.js are available is in the Environment Reference pages:

If you need more specific control of Node.js versions in your build, use any version installable by nvm. If your .travis.yml contains a version of Node.js that nvm cannot install, such as 0.4, the job errors immediately.

For a precise list of versions pre-installed on the VM, please consult “Build system information” in the build log.

Specifying Node.js versions using .nvmrc

Optionally, your repository can contain a .nvmrc file in the repository root to specify which single version of Node.js to run your tests against.

The .nvmrc file is only read when node_js key in your .travis.yml files does not specify a nodejs version. When the .nvmrc file is read, $TRAVIS_NODE_VERSION is set to the nodejs version. See nvm documentation for more information on .nvmrc.

Default Build Script

The default build script for projects using nodejs is:

npm test

Using other Test Suites

You can tell npm how to run your test suite by adding a line in package.json. For example, to test using Vows:

"scripts": {
  "test": "vows --spec"

Using Gulp

If you already use Gulp to manage your tests, install it and run the default gulpfile.js by adding the following lines to your .travis.yml:

  - npm install -g gulp-cli
script: gulp

Dependency Management

Travis CI uses npm

Travis CI uses npm to install your project dependencies:

npm install

Note that there are no npm packages installed by default in the Travis CI environment, your dependencies are downloaded and installed during each build.

Using a specific npm version

Add the following to the before_install phase of .travis.yml:

  - npm i -g npm@version-number

Caching with npm

Travis CI is able to cache the node_modules folder:

    - "node_modules"

npm install will still run on every build and will update/install any new packages added to your package.json file.

Travis CI supports yarn

Travis CI detects use of yarn.

If both package.json and yarn.lock are present in the root directory of the repository, we run the following command instead of npm install:


Note that yarn requires Node.js version 4 or later. If the job does not meet this requirement, npm install is used instead.

Using a specific yarn version

Add the following to the before_install phase of .travis.yml:

  - curl -o- -L | bash -s -- --version version-number
  - export PATH="$HOME/.yarn/bin:$PATH"

Caching with yarn

You can cache $HOME/.cache/yarn with:

cache: yarn

If your caching needs to include other directives, you can use:

  yarn: true

For more information, refer to Caching documentation.

Using shrinkwrapped git dependencies

Note that npm install can fail if a shrinkwrapped git dependency pointing to a branch has its HEAD changed.

Ember Apps

You can build your Ember applications on Travis CI. The default test framework is Qunit. The following example shows how to build and test against different Ember versions.

sudo: required
dist: trusty
      - google-chrome
      - google-chrome-stable
language: node_js
  - "7"
    - EMBER_VERSION=default
    - EMBER_VERSION=release
    - EMBER_VERSION=beta
    - EMBER_VERSION=canary
  fast_finish: true
    - env: EMBER_VERSION=release
    - env: EMBER_VERSION=beta
    - env: EMBER_VERSION=canary

    # setting the path for phantom.js 2.0.0
    - export PATH=/usr/local/phantomjs-2.0.0/bin:$PATH
    # starting a GUI to run tests, per
    - export DISPLAY=:99.0
    - sh -e /etc/init.d/xvfb start
    - "npm config set spin false"
    - "npm install -g npm@^2"
    - mkdir travis-phantomjs
    - wget -O $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2
    - tar -xvf $PWD/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -C $PWD/travis-phantomjs
    - export PATH=$PWD/travis-phantomjs:$PATH
    - npm install -g bower
    - npm install
    - bower install
    - ember test --server

Meteor Apps

You can build your Meteor Apps on Travis CI and test against laika:

language: node_js
  - "7"
  - "curl -L | /bin/sh"
  - mongodb
  - LAIKA_OPTIONS="-t 5000"

More info on testing against laika.

Meteor Packages

You can also build your Meteor Packages on Travis CI by extending the Node.js configuration.

The following before_install script installs the required dependencies:

language: node_js
  - "7"
  - "curl -L | /bin/sh"
  - "export PATH=$HOME/.meteor:$PATH"

Find the source code at travis-ci-meteor-packages.

Build Matrix

For JavaScript/Node.js projects, env and node_js can be used as arrays to construct a build matrix.

Node.js v4 (or io.js v3) compiler requirements

To compile native modules for io.js v3 or Node.js v4 or later, a C++11 standard-compliant compiler is required. More specifically, either gcc 4.8 (or later), or clang 3.5 (or later) works.

Our Trusty images have gcc and clang that meet this requirement, but the Precise image does not.

To update these compilers to a newer version, for example, gcc/g++ to version 4.8, add the following in your .travis.yml:

language: node_js
  - "4"
  - CXX=g++-4.8
      - ubuntu-toolchain-r-test
      - g++-4.8