You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CircleCI不同作业间共享npm install结果以避免重复执行?

Absolutely! You can cut down on that redundant npm install run by sharing the dependency installation results between your build and deploy jobs in CircleCI. There are two solid approaches to do this—Workspaces and Dependency Caching—let’s walk through both with your config.

Using CircleCI Workspaces

Workspaces let you share files directly between jobs in the same workflow. This is perfect if your deploy job always runs right after build:

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:7.10
    steps:
      - checkout
      - run: npm install
      - run: npm run lint
      # Save the node_modules folder to a shared workspace
      - persist_to_workspace:
          root: .
          paths:
            - node_modules

  deploy:
    machine: true
    steps:
      - checkout
      # Pull the pre-installed node_modules from the workspace
      - attach_workspace:
          at: .
      - run: npm run build

How it works:

  • The persist_to_workspace step in the build job saves your node_modules directory to a workspace that’s accessible by subsequent jobs.
  • The attach_workspace step in deploy pulls that folder into the job’s environment, so you can skip npm install entirely and jump straight to your build command.

Using Dependency Caching

Caching stores your node_modules in CircleCI’s persistent storage, which works well if jobs might run independently or you want to reuse dependencies across multiple workflows:

version: 2
jobs:
  build:
    docker:
      - image: circleci/node:7.10
    steps:
      - checkout
      # Try to restore cached node_modules first
      - restore_cache:
          keys:
            # Use package-lock.json checksum to ensure cache matches dependencies
            - v1-dependencies-{{ checksum "package-lock.json" }}
            # Fallback to latest cache if no exact match exists
            - v1-dependencies-
      - run: npm install
      # Save the cache after successful installation
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package-lock.json" }}
      - run: npm run lint

  deploy:
    machine: true
    steps:
      - checkout
      # Restore the same cache used by the build job
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package-lock.json" }}
            - v1-dependencies-
      - run: npm run build

How it works:

  • The restore_cache step checks for a cached node_modules folder tied to your package-lock.json (or package.json) checksum. If found, it skips the full npm install.
  • If no cache exists (e.g., first run or dependency changes), npm install runs normally, and the save_cache step stores the new node_modules for future jobs.

Which should you choose?

  • Workspaces: Best for sequential jobs where deploy always follows build—it’s a fast, direct file transfer.
  • Caching: Better for independent jobs or reusing dependencies across multiple workflows (like PR builds and main branch deployments).

内容的提问来源于stack exchange,提问作者LiranC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:08:09