Search code examples
node.jscachingnpmcontinuous-integration

Is there a way to speedup npm ci using cache?


for now npm ci is the most common way to install node modules when using CI. But it is honestly really slow. Is there a way to speedup npm ci using cache or do not fully remove existing packages (whole node_modules folder)?


Solution

  • NPM cache is located in ~/.npm but in most CIs you can only cache things inside your working directory.

    What you can do to circumvent this is changing the cache directory to your current directory with npm set cache .npm. The NPM cache will now be located in ./.npm an you can cache this folder between CI jobs.

    Example with GitLab CI:

    my-super-job:
      image: node:13-alpine
      script:
        - npm set cache .npm
        - npm ci 
      cache:
        paths:
          - .npm
    

    EDIT: Just discovered that you can set the config as a command line flag so npm ci --cache .npm should do the same