Home / FAQ / How to configure npm build cache for nodejs build nodes in Jenkins?

How to configure npm build cache for nodejs build nodes in Jenkins?

After configuring the build cache for the build nodes of a Jenkins instance, reusable downloaded dependencies can be used in the Jenkins pipeline build process, reducing build time, effectively improving pipeline build efficiency, lowering network bandwidth consumption, and controlling dependencies.

This article will explain how to configure npm build cache for Node.js build nodes in your Jenkins instance.

Introduction to npm

npm (Node Package Manager) is a package management tool for JavaScript, used to manage and distribute JavaScript dependencies.

When used as a build cache for Node.js build nodes, the following scenarios can be satisfied:

Prerequisites

Procedure

  1. After entering the Platform Management view, click on Toolchain Management > Instance Management in the left navigation bar.

  2. Click on the name of your Jenkins instance.

  3. Under the Build Nodes tab, click on the Node.js node to be configured for caching.

  4. Click on Update Build Node.

  5. Add a Build Cache and configure the relevant parameters according to the following instructions.

    Storage Type Parameter Description Notes
    Host Path Volume Host Path: /root/.npm
    Cache Type: Custom
    Cache Path: For example: /root/.npm
    The actual storage location is the path on the node where the task Pod is running. If the scheduling node is not fixed, the cache is invalid, so it is recommended to use the Persistent Volume Claim type of storage.
    Persistent Volume Claim Cache Type: Custom
    Cache Path: For example: /root/.npm
    Indicates that the selected PVC will be mounted to the /root/.npm directory of the container at runtime.
    PVC may be slower than downloading directly from the dependency repository when reading and writing small files. Therefore, when using PVC as a cache, make sure the storage system has sufficient performance.
  6. Click Update.

Verify if the cache is effective

  1. Switch to the DevOps view, click on CI/CD > Jenkins in the left navigation bar.

  2. Create a new pipeline or update an existing one that includes a Node.js build task.

  3. Select the Node.js node configured with the build cache as the Build Node.

  4. In the build command of the Node.js build task, enter the following command to configure the cache path.

    Note: Replace nodejs’s cache path in the command with the Cache Path configured for the Node.js build node in the previous steps.

    npm set cache=<nodejs’s cache path>
  5. Execute the pipeline.

  6. Log in to the node where the Jenkins instance component’s Pod is located. If a cache file _cacache has been generated in the Cache Path configured for the Node.js build node mentioned above, it indicates that the build cache configuration is successful and effective.