> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-integration/troubleshooting-and-resources/tutorials-and-code-samples/ci-nodejs.md).

# NodeJS

You can build and test a [Node.js](https://nodejs.org/en/docs/guides/getting-started-guide) application using a Linux platform on [Harness Cloud](/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/use-harness-cloud-build-infrastructure.md) or a [self-managed Kubernetes cluster](/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/k8s-build-infrastructure.md) build infrastructure.

This guide assumes you've [created a Harness CI pipeline](/continuous-integration/use-harness-ci/use-harness-ci/prep-ci-pipeline-components.md).

### Install dependencies <a href="#install-dependencies" id="install-dependencies"></a>

Use [Run step](/continuous-integration/use-harness-ci/use-harness-ci/run-step-settings.md) to install dependencies in the build environment.

{% tabs %}
{% tab title="Harness Cloud" %}

```yaml
- step:
    type: Run
    identifier: dependencies
    name: Dependencies
    spec:
      shell: Sh
      command: |-
        npm install express@4.18.2 --no-save
```

{% endtab %}

{% tab title="Self-managed" %}

```yaml
- step:
    type: Run
    identifier: dependencies
    name: Dependencies
    spec:
      connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
      image: node:14.18.2-alpine
      command: |-
        npm install express@14.18.2 --no-save
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
In addition to Run steps, [Plugin steps](/continuous-integration/use-harness-ci/use-harness-ci/use-drone-plugins/explore-ci-plugins.md) are also useful for installing dependencies.

You can use [Background steps](/continuous-integration/use-harness-ci/use-harness-ci/manage-dependencies/background-step-settings.md) to run dependent services that are needed by multiple steps in the same stage.
{% endhint %}

### Cache dependencies <a href="#cache-dependencies" id="cache-dependencies"></a>

{% tabs %}
{% tab title="Cache Intelligence" %}
Cache Node dependencies with [Cache Intelligence](/continuous-integration/use-harness-ci/use-harness-ci/caching-ci-data/cache-intelligence.md).

Add `caching.enabled.true` to your `stage.spec`:

```yaml
- stage:
    spec:
      caching:
        enabled: true
```

{% endtab %}

{% tab title="Save and Restore Cache steps" %}
You can use built-in steps to:

* [Save and Restore Cache from S3](/continuous-integration/use-harness-ci/use-harness-ci/caching-ci-data/saving-cache.md)
* [Save and Restore Cache from GCS](/continuous-integration/use-harness-ci/use-harness-ci/caching-ci-data/save-cache-in-gcs.md)

{% hint style="info" %}
**NODE CACHE KEY AND PATH REQUIREMENTS**

All Node pipelines must include `node_modules` in the `sourcePaths` for your **Save Cache** step.

```yaml
spec:
  sourcePaths:
    - node_modules
```

If your pipeline uses [npm](https://www.npmjs.com/), the `key` value must reference `package-lock.json` in your **Save Cache** and **Restore Cache** steps.

```yaml
spec:
  key: cache-{{ checksum "package-lock.json" }}
```

If your pipeline uses [yarn](https://yarnpkg.com/), the `key` value must reference `yarn.lock` in your **Save Cache** and **Restore Cache** steps.

```yaml
spec:
  key: cache-{{ checksum "yarn.lock" }}
```

{% endhint %}

<details>

<summary>YAML example: Save and restore cache steps</summary>

Here's an example of a pipeline with **Save Cache to S3** and **Restore Cache from S3** steps.

```yaml
            steps:
              - step:
                  type: RestoreCacheS3
                  name: Restore Cache From S3
                  identifier: Restore_Cache_From_S3
                  spec:
                    connectorRef: AWS_Connector
                    region: us-east-1
                    bucket: your-s3-bucket
                    key: cache-{{ checksum "package-lock.json" }}
                    archiveFormat: Tar
              - step:
                  type: Run
                  ...
              - step:
                  type: BuildAndPushDockerRegistry
                  ...
              - step:
                  type: SaveCacheS3
                  name: Save Cache to S3
                  identifier: Save_Cache_to_S3
                  spec:
                    connectorRef: AWS_Connector
                    region: us-east-1
                    bucket: your-s3-bucket
                    key: cache-{{ checksum "package-lock.json" }}
                    sourcePaths:
                      - node_modules
                    archiveFormat: Tar
```

</details>
{% endtab %}
{% endtabs %}

### Build and run tests <a href="#build-and-run-tests" id="build-and-run-tests"></a>

Add [Run steps](/continuous-integration/use-harness-ci/use-harness-ci/run-step-settings.md) to build and [run tests in Harness CI](/continuous-integration/use-harness-ci/use-harness-ci/run-tests/run-tests-in-ci.md).

{% tabs %}
{% tab title="Harness Cloud" %}

```yaml
- step:
    type: Run
    name: npm test
    identifier: npm_test
    spec:
      shell: Sh
      command: |-
        npm install
        npm run build --if-present
        npm test
      reports:
        type: JUnit
        spec:
          paths:
            - report.xml
```

{% endtab %}

{% tab title="Self-managed" %}

```yaml
- step:
    type: Run
    name: npm test
    identifier: npm test
    spec:
      connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
      image: node:latest
      shell: Sh
      command: |-
        npm install
        npm run build --if-present
        npm test
      reports:
        type: JUnit
        spec:
          paths:
            - report.xml
```

{% endtab %}
{% endtabs %}

#### Visualize test results <a href="#visualize-test-results" id="visualize-test-results"></a>

If you want to [view test results in Harness](/continuous-integration/use-harness-ci/use-harness-ci/run-tests/viewing-tests.md), make sure your test commands produce reports in JUnit XML format and that your steps include the `reports` specification.

```yaml
reports:
  type: JUnit
  spec:
    paths:
      - report.xml
```

#### Test splitting <a href="#test-splitting" id="test-splitting"></a>

You can use [test splitting (parallelism)](/continuous-integration/use-harness-ci/use-harness-ci/run-tests/speed-up-ci-test-pipelines-using-parallelism.md) to improve test times.

### Specify version <a href="#specify-version" id="specify-version"></a>

{% tabs %}
{% tab title="Harness Cloud" %}
Node is pre-installed on Hosted Cloud runners. For details about all available tools and versions, go to [Platforms and image specifications](/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/use-harness-cloud-build-infrastructure.md#platforms-and-image-specifications).

If your application requires a specific Node version, add a **Run** step to install it.

<details>

<summary>Install one Node version</summary>

```yaml
- step:
    type: Run
    name: Install Node
    identifier: installnode
    spec:
      shell: Sh
      envVariables:
        NODE_VERSION: 18.16.0
      command: |-
        mkdir $HOME/nodejs
        curl -L https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz | tar xJ -C $HOME/nodejs
        export PATH=$HOME/nodejs/node-v${NODE_VERSION}-linux-x64/bin:$PATH
```

</details>

<details>

<summary>Install multiple Node versions</summary>

1. Add the [matrix looping strategy](/harness-ai/use-harness-platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism.md) configuration to your stage.

```yaml
- stage:
    strategy:
      matrix:
        nodeVersion:
          - 18.16.0
          - 20.2.0
```

2. Reference the matrix variable in your steps.

```yaml
- step:
    type: Run
    name: Install node
    identifier: installnode
    spec:
      shell: Sh
      command: |-
        mkdir $HOME/nodejs
        curl -L https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.xz | tar xJ -C $HOME/nodejs
        export PATH=$HOME/nodejs/node-v${NODE_VERSION}-linux-x64/bin:$PATH
      envVariables:
        NODE_VERSION: <+matrix.nodeVersion>
```

</details>
{% endtab %}

{% tab title="Self-managed" %}
Specify the desired [Node Docker image](https://hub.docker.com/_/node) tag in your steps. There is no need for a separate install step when using Docker.

<details>

<summary>Use a specific Node version</summary>

```yaml
- step:
    type: Run
    name: Node Version
    identifier: nodeversion
    spec:
      connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
      image: node:18.16.0
      shell: Sh
      command: |-
        npm version
```

</details>

<details>

<summary>Use multiple node versions</summary>

1. Add the [matrix looping strategy](/harness-ai/use-harness-platform/pipelines/looping-strategies/looping-strategies-matrix-repeat-and-parallelism.md) configuration to your stage.

```yaml
- stage:
    strategy:
      matrix:
        nodeVersion:
          - 18.16.0
          - 20.2.0
```

2. Reference the matrix variable in the `image` field of your steps.

```yaml
- step:
    type: Run
    name: Node Version
    identifier: nodeversion
    spec:
      connectorRef: YOUR_IMAGE_REGISTRY_CONNECTOR
      image: node:<+matrix.nodeVersion>
      shell: Sh
      command: |-
        npm version
```

</details>
{% endtab %}
{% endtabs %}

### Full pipeline examples <a href="#full-pipeline-examples" id="full-pipeline-examples"></a>

Here's a YAML example of a pipeline that:

1. Tests a Node code repo.
2. Builds and pushes an image to Docker Hub.

This pipeline uses [Harness Cloud build infrastructure](/continuous-integration/use-harness-ci/use-harness-ci/set-up-build-infrastructure/use-harness-cloud-build-infrastructure.md) and [Cache Intelligence](/continuous-integration/use-harness-ci/use-harness-ci/caching-ci-data/cache-intelligence.md).

If you copy this example, replace the placeholder values with appropriate values for your Harness project, connector IDs, account/user names, and repo names.

<details>

<summary>Pipeline YAML</summary>

```yaml
pipeline:
  name: nodejs-sample
  identifier: nodejssample
  projectIdentifier: default
  orgIdentifier: default
  tags: {}
  stages:
    - stage:
        name: Build Node App
        identifier: Build_Node_App
        description: ""
        type: CI
        spec:
          cloneCodebase: true
          caching:
            enabled: true
          platform:
            os: Linux
            arch: Amd64
          runtime:
            type: Cloud
            spec: {}
          execution:
            steps:
              - step:
                  type: Run
                  name: npm test
                  identifier: npm_test
                  spec:
                    shell: Sh
                    command: |-
                      npm install
                      npm run build --if-present
                      npm test
              - step:
                  type: BuildAndPushDockerRegistry
                  name: BuildAndPushDockerRegistry_1
                  identifier: BuildAndPushDockerRegistry_1
                  spec:
                    connectorRef: YOUR_DOCKER_CONNECTOR_ID
                    repo: YOUR_DOCKER_HUB_USERNAME/DOCKER_REPO_NAME
                    tags:
                      - <+pipeline.sequenceId>
  properties:
    ci:
      codebase:
        connectorRef: YOUR_CODE_REPO_CONNECTOR_ID
        repoName: YOUR_REPO_NAME
        build: <+input>
```

</details>

### Next steps <a href="#next-steps" id="next-steps"></a>

Now that you have created a pipeline that builds and tests a Node app, you could:

* Create [triggers](/harness-ai/use-harness-platform/triggers.md) to automatically run your pipeline.
* Add steps to [build and upload artifacts](/continuous-integration/use-harness-ci/use-harness-ci/build-and-upload-artifacts.md).
* Add a step to [build and push an image to a Docker registry](/continuous-integration/use-harness-ci/use-harness-ci/build-and-upload-artifacts/build-and-push/build-and-push-to-docker-registry.md).
