Build a JavaScript and Node.js project

For Language versions and other build-environment specific information visit our reference pages:

This guide covers build environment and configuration topics specific to JavaScript and Node.js projects. Please make sure to read our Onboarding and General Build configuration guides first.

Specify 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
  • lts/* latest LTS Node.js release

  • 25 latest 25.x release

  • 24 latest 24.x release

  • 23 latest 23.x release

  • 22 latest 22.x release

  • 21 latest 21.x release
language: node_js node_js:   - 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 that is 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.

Specify 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 the 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 

In the case where no package.json file is present in the root folder, the default build script is:

make test 

Yarn Support #

If yarn.lock exists, the default test command will be yarn test instead of npm test.

Use 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" }, 

Use 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:

before_script:   - npm install -g gulp-cli script: gulp 

Dependency Management #

Travis CI uses npm or yarn to install your project dependencies.

Note that there are no npm packages installed by default in the Travis CI environment.

Use npm #

Use a specific npm version #

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

before_install:   - npm i -g npm@version-number 

Support for npm ci #

If package-lock.json or npm-shrinkwrap.json exists and your npm version supports it, Travis CI will use npm ci instead of npm install.

This command will delete your node_modules folder and install all dependencies as specified in your lock file.

Cache with npm #

npm is now cached by default. In case you want to disable it, please add the following to your .travis.yml:

cache:   npm: false 

To explicitly cache your dependencies:

cache: npm 
  1. This cache’s $HOME/.npm precisely when npm ci is the default script` command. (See above.)

  2. In all other cases, this will cache node_modules. Note that npm install will still run on every build and will update/install any new packages added to your package.json file.

Even when script is overridden, this shortcut is effective.

Use yarn #

Travis CI detects the use of yarn.

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

yarn --frozen-lockfile 

If your Yarn version does not support --frozen-lockfile, we run just yarn.

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

Use a specific yarn version #

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

before_install:   - curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version version-number   - export PATH="$HOME/.yarn/bin:$PATH" 

Cache with yarn #

cache: yarn 

will add yarn’s default caching directory (which varies depending on the OS), as indicated by yarn cache dir.

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

cache:   yarn: true 

For more information, refer to Caching documentation.

Use shrinkwrapped git dependencies #

Note that npm install can fail if a shrink-wrapped 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.

dist: focal addons:   apt:     sources:       - google-chrome     packages:       - google-chrome-stable language: node_js node_js:   - "7" env:     - EMBER_VERSION=default     - EMBER_VERSION=release     - EMBER_VERSION=beta     - EMBER_VERSION=canary jobs:   fast_finish: true   allow_failures:     - env: EMBER_VERSION=release     - env: EMBER_VERSION=beta     - env: EMBER_VERSION=canary  before_install:     # 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 https://docs.travis-ci.com/user/gui-and-headless-browsers/#using-xvfb-to-run-tests-that-require-a-gui     - export DISPLAY=:99.0     - sh -e /etc/init.d/xvfb start     - "npm config set spin false"     - "npm install -g npm@^2" install:     - mkdir travis-phantomjs     - wget https://s3.amazonaws.com/travis-phantomjs/phantomjs-2.0.0-ubuntu-12.04.tar.bz2 -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 script:     - ember test --server 

Meteor Apps #

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

language: node_js node_js:   - "7" before_install:   - "curl -L https://raw.githubusercontent.com/arunoda/travis-ci-laika/6a3a7afc21be99f1afedbd2856d060a02755de6d/configure.sh | /bin/sh" services:   - mongodb env:   - 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 node_js:   - "7" before_install:   - "curl -L https://raw.githubusercontent.com/arunoda/travis-ci-meteor-packages/dca8e51fafd60d9e5a8285b07ca34a63f22a5ed4/configure.sh | /bin/sh" before_script:   - "export PATH=$HOME/.meteor:$PATH" 

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

Build Config Reference #

You can find more information on the build config format for Javascript in our Travis CI Build Config Reference.