New to Bitbucket Cloud? Check out our get started guides for new users.
This guide shows you how to use Bitbucket Pipelines for building and testing a Node.js software project in a Docker container.
If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo node repo.
If you'd like to set it up by hand, most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.
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 10.15.0 by specifying it at the beginning of your bitbucket-pipelines.yml file.
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:
To find a list of all supported Node.js versions and corresponding image tags, please refer to https://hub.docker.com/_/node/.
You can check your bitbucket-pipelines.yml file with our online validator.
Manage 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:
Manage dependencies with Yarn
The default Node.js image on Docker Hub comes with yarn installed by default. You can use it directly in your script to install specific packages locally or globally.
Install 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:
or use yarn global add if you're using Yarn.
Download and publish 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 using repository variables. 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.
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 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.
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.
Use a service container to run a database in your pipeline.
Now you've built and tested your project you might want to deploy it somewhere. Have a look at our deployment guides for your next steps.