Javascript (Node.js) with Bitbucket Pipelines

This guide will help you understand how to configure your bitbucket-pipelines.yml file to build and test a Node.js project.

Specify your Node.js version with Docker

Bitbucket Pipelines runs all your builds in Docker containers using an image that you provide at the beginning of your configuration file. You can easily use Node.js with Bitbucket Pipelines by using one of the official Node.js Docker images on Docker Hub. 

For instance, you can use Node.js 6.9.4 by specifying it at the beginning of your bitbucket-pipelines.yml file.

image: node:6.9.4
pipelines:
  default:
    - step:
        script:
          - node -v

If you wanted to use a different version of Node.js you simply need to change the tag of the Node.js Docker image. The example below would start a container with Node 4.7.2.

image: node:4.7.2

To find a list of all supported Node.js versions and corresponding image tags, please refer to https://hub.docker.com/_/node/.

Installing dependencies

Managing dependencies with npm

The default Node.js image on Docker Hub comes with npm installed by default. You can use it directly in your script to install specific packages locally or globally.

If you are using a package.json file, you can simply run npm install at the beginning of your script to install all the required dependencies.

image: node:6.9.4
pipelines:
  default:
    - step:
        script:
          - npm install

Managing dependencies with Yarn

You will have to install Yarn as part of your build script to be able to use it. You can simply copy and paste the instructions in the example below to 

image: node:6.9.4
pipelines:
  default:
    - step:
        script:
          - curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.18.1
          - export PATH=$HOME/.yarn/bin:$PATH
          - yarn

Installing dependencies globally (Gulp, Grunt, Angular CLI...)

Node packages can also be installed globally. For instance, if your pipeline depends on a task runner like Gulp or Grunt, you will need to install them globally. You can use the npm install -g command if you're using npm as your package manager.

npm install -g grunt-cli
npm install
grunt

or yarn global add if you're using Yarn.

yarn global add grunt-cli
yarn
grunt

Download and publishing modules to a private registry

If you are using private modules from either the public npm registry or your own private registry you can simply check in a .npmrc file at the root of your repository with the using environment variables. In order to avoid conflict with the local ~/.npmrc file during development you can simply name it .npmrc_config and rename it as part of the pipeline script.

.npmrc_config
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org

If you are using a private npm registry you just need to replace registry.npmjs.org with the address of your registry.

Then configure the following environment variable in Bitbucket Pipelines:

  • NPM_TOKEN: This is the authentication token to your registry. You can find it in your local ~/.npmrc, after you login to your registry.

Your bitbucket-pipelines.yml script will then look like the following to install your private dependencies.

bitbucket-pipelines.yml
image: node:6.9.4
pipelines:
  default:
    - step:
        script:
          - mv .npmrc_config .npmrc
          - npm install

Databases

It is not yet possible to use multiple containers in a running pipeline, but you can use or create a Docker image that contains both Node.js and the required database. You will find below some examples of images that you can re-use or adapt to meet your requirements.

Testing

You simply need to add to your bitbucket-pipelines.yml file the same commands that you would run locally to test your application. For instance, if you are running npm test locally to run your test suite the following example should work for you.

image: node:6.9.4
pipelines:
  default:
    - step:
        script:
          - npm install
          - npm test

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport