JavaScript and Node.js

Supported Node.js versions

Semaphore uses nvm to manage Node.js versions. Any version installable with nvm is supported on Semaphore. Version 8.11 is pre-installed. The default version is set from .nvmrc file in your repo. You can change this by calling sem-version node. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sem-version node 10.13.0
      jobs:
        - name: Tests
          commands:
            - node --version

If you need a version other than the preinstalled versions, then you can install it with nvm. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - nvm install --lts carbon
          - sem-version node --lts carbon
      jobs:
        - name: Tests
          commands:
            - node --version

Dependency caching

You can use Semaphores cache command to store and load node_modules. In the following configuration example, we install dependencies and warm the cache in the first block, then use the cache in subsequent blocks.

version: "v1.0"
name: First pipeline example
agent:
  machine:
    type: e1-standard-2
    os_image: ubuntu1804

blocks:
  - name: Install dependencies
    task:
      jobs:
        - name: npm install and cache
          commands:
            - checkout
            - cache restore node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json),node-modules-$SEMAPHORE_GIT_BRANCH,node-modules-master
            - npm install
            - cache store node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json) node_modules

  - name: Tests
    task:
      prologue:
        commands:
          - checkout
          - cache restore node-modules-$SEMAPHORE_GIT_BRANCH-$(checksum package-lock.json),node-modules-$SEMAPHORE_GIT_BRANCH,node-modules-master
      jobs:
        - name: Everything
          commands:
            - npm test

If you need to clear cache for your project, launch a debug session and execute cache clear or cache delete <key>.

Yarn is supported

Besides NPM, Semaphore also supports Yarn for managing Node.js dependencies.

To get started, use the configuration example above and replace package-lock.json with yarn.lock.

Environment Variables

Semaphore doesn't set language specific environment variables like NODE_ENV You should set these at the task level.

blocks:
  - name: Tests
    task:
      env_vars:
        - name: NODE_ENV
          value: test
      jobs:
        - name: Everything
          commands:
            - npm test

Browser testing

Install the selenium-webdriver library and it should work out of the box, same goes for higher level libraries that leverage Selenium. See the official Node examples.

Refer to the Ubuntu image reference for details on preinstalled browsers and testing tools on Semaphore.

Still need help? Contact Us Contact Us