A reliable CI pipeline should install the same dependency versions that developers use on their local machines. However, this consistency can break when GitHub Actions installs dependencies without respecting the project's lockfile or when dependency caches are restored using incomplete cache keys.

For example, a developer may run tests successfully because their local environment uses the exact package versions recorded in package-lock.json. A GitHub Actions workflow that uses a general dependency installation command may resolve newer compatible versions instead. The pipeline can then fail even though the developer has not changed the application code.

This problem is not limited to Node.js. Projects using npm, Yarn, pnpm, Python, .NET, and other package managers need a reliable way to keep dependency resolution consistent across local development and automated builds.

Lockfiles provide that foundation, but simply committing a lockfile is not enough. The workflow must use the correct installation command, keep the lockfile synchronized with dependency manifests, and configure caching without allowing stale or incompatible dependencies to affect builds.

Why Dependency Versions Can Differ Between Local and CI Environments

Most package managers allow developers to declare dependency version ranges rather than exact versions.

Consider the following package.json file:

JSON
{
  "name": "customer-portal",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.0",
    "axios": "^1.6.0"
  }
}

The caret character allows compatible updates within the specified major version range.

For example, a dependency declared as ^4.18.0 can resolve to a later compatible 4.x release, subject to the package manager's version-resolution rules.

This is convenient during development because developers can receive compatible dependency updates without changing package.json every time.

However, it introduces a problem when different environments resolve dependencies at different times.

Imagine this scenario:

  1. A developer installs dependencies on Monday.

  2. The package manager resolves the available compatible versions.

  3. The developer runs tests successfully.

  4. A new compatible dependency version is published on Wednesday.

  5. GitHub Actions runs a fresh installation on Thursday.

  6. The CI environment resolves the newer version.

  7. A test fails because the new dependency behaves differently.

The application code has not changed, but the dependency tree has.

This is one reason reproducible builds matter. A build should use a known dependency tree rather than independently resolving package versions every time it runs.

Understand What a Lockfile Actually Does

A lockfile records the resolved dependency tree used by a package manager.

For an npm project, the file is package-lock.json.

For Yarn, it is yarn.lock.

For pnpm, it is pnpm-lock.yaml.

Consider a simplified package-lock.json:

JSON
{
  "name": "customer-portal",
  "lockfileVersion": 3,
  "packages": {
    "": {
      "dependencies": {
        "express": "^4.18.0"
      }
    },
    "node_modules/express": {
      "version": "4.21.0",
      "resolved": "https://registry.npmjs.org/express/-/express-4.21.0.tgz",
      "integrity": "sha512-example"
    }
  }
}

The package manifest describes the dependency requirement, while the lockfile records the resolved package version and associated installation information.

The lockfile also contains information about transitive dependencies.

For example, your application may directly depend on Express, but Express itself depends on other packages. Those packages can have their own dependencies.

A lockfile helps preserve the complete resolved dependency tree instead of allowing every environment to independently choose versions.

Why should lockfiles be committed to Git?

Committing lockfiles provides several benefits:

  • Developers and CI environments use consistent dependency resolutions.

  • Dependency changes become visible in pull request diffs.

  • Builds become more predictable.

  • Unexpected dependency upgrades are easier to identify.

  • Rollbacks can restore a previously committed dependency tree.

  • Production deployments can use the same resolved dependencies as tested builds.

A lockfile should normally be committed alongside the corresponding dependency manifest.

For example:

Code
customer-portal/
│
├── package.json
├── package-lock.json
├── src/
├── tests/
└── .github/
    └── workflows/
        └── build.yml

Do not commit the node_modules directory simply to preserve dependency versions. The lockfile is the appropriate source-controlled representation of the dependency tree.

Use npm ci Instead of npm install in GitHub Actions

One of the most important changes you can make to an npm-based GitHub Actions workflow is replacing npm install with npm ci.

Both commands install dependencies, but they have different purposes.

npm install

The npm install command is commonly used during development.

Code
npm install

It can install dependencies and update package-lock.json when the dependency manifest and lockfile need to be reconciled.

This is useful when adding packages, updating versions, or changing dependency requirements.

However, it is not the preferred command for a CI installation that should preserve an existing lockfile.

npm ci

The npm ci command is designed for clean and automated installations.

Code
npm ci

Its important characteristics include:

  • Requires an existing package-lock.json or npm-shrinkwrap.json.

  • Fails if package.json and the lockfile describe incompatible dependency requirements.

  • Removes an existing node_modules directory before installation.

  • Does not modify package.json or package-lock.json.

  • Installs using the resolved dependency information in the lockfile.

This makes npm ci a better choice for repeatable CI builds.

A problematic GitHub Actions workflow

Consider this workflow:

Code
name: Node.js CI

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v7
        with:
          node-version: 24

      - name: Install dependencies
        run: npm install

      - name: Run tests
        run: npm test

The workflow uses npm install, which may reconcile the lockfile rather than treating it as an immutable installation contract.

A better GitHub Actions workflow

Replace npm install with npm ci:

VB.NET
name: Node.js CI

on:
  push:
    branches: [main]

  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

      - name: Build application
        run: npm run build

Now the workflow installs dependencies using the committed lockfile.

If package.json and package-lock.json are inconsistent, the installation fails instead of silently updating the lockfile.

That failure is useful. It tells the team that the dependency changes need to be resolved and committed before the pipeline can pass.

Configure GitHub Actions Caching Without Breaking Lockfile Consistency

Dependency installation can take considerable time, especially when an application has many direct and transitive dependencies.

GitHub Actions supports dependency caching to reduce repeated downloads.

For Node.js applications, actions/setup-node provides built-in caching for npm, Yarn, and pnpm.

Consider this configuration:

Code
- name: Set up Node.js
  uses: actions/setup-node@v7
  with:
    node-version: 24
    cache: npm

The setup-node action identifies the package manager's dependency file and uses its hash when creating the cache key.

For npm, this is generally package-lock.json.

The important distinction is that dependency caching and dependency installation are separate operations.

Caching restores package-manager data to make installation faster. The package manager still performs the installation and validates the dependency tree.

The setup-node action does not cache the node_modules directory. It caches package-manager data that can be reused across installations.

This means the recommended combination remains:

Code
- uses: actions/setup-node@v7
  with:
    node-version: 24
    cache: npm

- run: npm ci

You get the performance benefit of caching while keeping the installation controlled by the lockfile.

Why should the cache key depend on the lockfile?

Suppose the application changes a dependency from one version to another.

The package-lock.json file changes accordingly.

Because setup-node uses the lockfile hash in its cache key, a different dependency definition produces a different cache key.

This prevents the workflow from treating a cache created for an older lockfile as an exact match for the new dependency state.

The lockfile remains the source of truth. The cache is only an optimization.

Avoid manually caching node_modules without a clear reason

A common custom caching approach looks like this:

Code
- name: Cache node_modules
  uses: actions/cache@v5
  with:
    path: node_modules
    key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}

This is generally unnecessary when using setup-node's built-in caching.

Caching node_modules can introduce additional complexity involving operating system differences, Node.js versions, native dependencies, and installation lifecycle scripts.

Prefer caching the package manager's download data and running npm ci for every job.

This keeps the installation process predictable.

Keep Yarn and pnpm Lockfiles Consistent

The same principle applies when your project uses Yarn or pnpm.

Each package manager provides an installation mode that prevents CI from modifying the lockfile.

Yarn projects

Yarn Berry provides the immutable installation option:

Code
yarn install --immutable

This command fails if the installation would require modifying the lockfile.

For older Yarn Classic projects, use:

Code
yarn install --frozen-lockfile

A GitHub Actions workflow for Yarn can look like this:

Code
name: Yarn CI

on:
  push:
    branches: [main]

  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: yarn

      - name: Install dependencies
        run: yarn install --immutable

      - name: Run tests
        run: yarn test

      - name: Build application
        run: yarn build

Use the command appropriate for the Yarn version configured in the repository.

pnpm projects

pnpm supports frozen lockfile installation:

Code
pnpm install --frozen-lockfile

This prevents the package manager from updating pnpm-lock.yaml during CI installation.

A workflow can look like this:

SQL
name: pnpm CI

on:
  push:
    branches: [main]

  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v6

      - name: Set up pnpm
        uses: pnpm/action-setup@v6
        with:
          version: 10

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 24
          cache: pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run tests
        run: pnpm test

      - name: Build application
        run: pnpm build

Recent pnpm versions automatically enable frozen-lockfile behavior in CI when a lockfile is present. Explicitly specifying the option makes the workflow's intention clear and avoids depending on implicit behavior.

Compare installation commands

Package manager

Recommended CI command

Lockfile

npm

npm ci

package-lock.json

Yarn Berry

yarn install --immutable

yarn.lock

Yarn Classic

yarn install --frozen-lockfile

yarn.lock

pnpm

pnpm install --frozen-lockfile

pnpm-lock.yaml

The key requirement is that CI should fail when the committed dependency definition cannot be installed without changing the lockfile.

Handle Lockfiles Correctly in Monorepositories

Monorepositories introduce another challenge.

A repository may contain multiple applications, each with its own dependency manifest and lockfile.

For example:

Java
company-platform/
│
├── frontend/
│   ├── package.json
│   └── package-lock.json
│
├── admin/
│   ├── package.json
│   └── package-lock.json
│
├── mobile-web/
│   ├── package.json
│   └── package-lock.json
│
└── .github/
    └── workflows/
        └── build.yml

If the workflow caches dependencies using only the root package-lock.json, it may not track changes to the lockfiles inside individual applications.

The cache key must reflect the dependency files used by the workflow.

With setup-node, you can configure cache-dependency-path:

Code
- name: Set up Node.js
  uses: actions/setup-node@v7
  with:
    node-version: 24
    cache: npm
    cache-dependency-path: |
      frontend/package-lock.json
      admin/package-lock.json
      mobile-web/package-lock.json

Then install dependencies separately:

Code
- name: Install frontend dependencies
  working-directory: frontend
  run: npm ci

- name: Install admin dependencies
  working-directory: admin
  run: npm ci

- name: Install mobile web dependencies
  working-directory: mobile-web
  run: npm ci

This approach is useful when each application maintains an independent lockfile.

What if the monorepository uses a shared lockfile?

Some monorepositories use workspaces with a single root lockfile.

For example:

Code
company-platform/
│
├── package.json
├── package-lock.json
│
├── apps/
│   ├── customer-portal/
│   └── admin-portal/
│
└── packages/
    ├── shared-ui/
    └── utilities/

In this situation, the root lockfile represents the workspace dependency tree.

You should install dependencies from the workspace root:

Code
- name: Install workspace dependencies
  run: npm ci

Do not independently run npm ci inside every workspace package unless the repository is intentionally designed with separate lockfiles.

For shared-lockfile workspaces, use the package manager's workspace installation behavior.

Pin the Package Manager Version

A lockfile alone does not guarantee that every environment behaves identically.

Different package manager versions can interpret configuration and lockfile formats differently.

For example, a project might be developed using one pnpm version while GitHub Actions installs another version.

This can introduce unexpected differences in dependency installation.

You can declare the package manager in package.json:

JSON
{
  "name": "customer-portal",
  "version": "1.0.0",
  "packageManager": "[email protected]"
}

Use the version that your project actually supports and has validated.

You can also pin the Node.js runtime version in the workflow:

Code
- name: Set up Node.js
  uses: actions/setup-node@v7
  with:
    node-version: 24

For additional consistency, use a committed runtime version file such as .nvmrc:

Code
24

Then configure the action to read it:

Code
- name: Set up Node.js
  uses: actions/setup-node@v7
  with:
    node-version-file: .nvmrc
    cache: npm

The runtime version and package manager version are separate from the lockfile. Both should be managed deliberately.

For stronger reproducibility, pin the exact runtime and package manager versions rather than relying on floating versions such as latest.

Keep Dependency Installation Configuration Consistent

Another source of CI failures is using different package manager configuration between local development and GitHub Actions.

Consider an npm project that uses this command locally:

Code
npm install --legacy-peer-deps

But the CI workflow runs:

Code
npm ci

The installation may fail because npm ci needs compatible installation settings when the lockfile was created using options that affect dependency-tree construction.

The npm documentation specifically calls out options such as legacy-peer-deps and install-links.

Instead of relying on developers to remember command-line flags, store the required configuration in the project.

For example, create a committed .npmrc file:

Code
legacy-peer-deps=true

Now local development and CI can use the same installation configuration.

This is particularly important for projects with complex peer dependency requirements.

Do not add configuration flags simply to suppress installation errors. Understand why the dependency tree requires them and document the decision.

Validate Lockfile Changes in Pull Requests

A lockfile should not change unexpectedly during CI.

One useful practice is to make dependency consistency part of pull request validation.

For npm projects, npm ci already verifies that the manifest and lockfile are compatible.

You can add an explicit check to ensure the lockfile was not modified during the installation process:

Code
- name: Install dependencies
  run: npm ci

- name: Verify lockfile consistency
  run: git diff --exit-code -- package.json package-lock.json

This check is useful as an additional safeguard, although npm ci itself should not modify these files.

For Yarn and pnpm, their immutable or frozen installation modes provide similar protection.

A failed consistency check should be treated as a repository maintenance issue.

The developer should update the dependency manifest and lockfile together locally, review the resulting changes, and commit both files.

For example:

Code
npm install axios@latest

After reviewing the updated package.json and package-lock.json:

Code
git status
git diff -- package.json package-lock.json

The pull request should contain the intended dependency changes rather than relying on CI to generate them.

Protect Dependency Caches in Workflows That Use Secrets

Dependency caching can improve build speed, but cache security also matters.

GitHub Actions caches can be restored by workflows under certain branch and pull request conditions.

If a workflow handles sensitive credentials or runs scripts from untrusted pull requests, blindly restoring or saving caches requires careful consideration.

Package installation can execute lifecycle scripts. Some packages run scripts during installation, so restoring dependencies from an untrusted cache can create a security concern.

For workflows with elevated privileges or access to sensitive information:

  • Avoid restoring caches from untrusted sources.

  • Do not expose deployment secrets to untrusted pull request code.

  • Consider disabling automatic package-manager caching when it is not needed.

  • Use restricted permissions for workflow jobs.

  • Review dependency installation scripts and package provenance.

  • Keep third-party GitHub Actions pinned to reviewed versions or full commit SHAs according to your security policy.

For example, setup-node supports disabling automatic npm caching:

Code
- name: Set up Node.js
  uses: actions/setup-node@v7
  with:
    node-version: 24
    package-manager-cache: false

Caching is an optimization, not a requirement for reproducible builds.

Security-sensitive workflows should prioritize trusted inputs and controlled execution over installation speed.

Common Mistakes That Cause Dependency Inconsistency

Mistake

Why it causes problems

Better approach

Using npm install in CI

Can update the lockfile during installation

Use npm ci

Not committing lockfiles

Different environments may resolve different versions

Commit the package manager's lockfile

Using the wrong cache dependency path

Cache keys may not reflect actual dependency changes

Configure cache-dependency-path

Caching node_modules unnecessarily

Can introduce environment-specific cache problems

Cache package-manager download data

Using different package manager versions

Installation behavior may differ

Declare and pin the package manager version

Ignoring .npmrc or other configuration

CI may build a different dependency tree

Commit required project-level configuration

Updating package.json without updating the lockfile

Frozen installation fails

Update and commit both files

Using an incorrect workspace installation command

Packages may not receive the intended shared dependency tree

Follow the workspace's lockfile structure

Trusting caches from untrusted workflows

Can introduce supply-chain risks

Apply appropriate cache and permission controls

Best Practices for Production-Ready GitHub Actions Workflows

For reliable dependency management, follow these practices:

  • Commit the appropriate lockfile for every application or workspace.

  • Use npm ci, Yarn immutable installation, or pnpm frozen-lockfile installation in CI.

  • Pin the Node.js runtime and package manager versions.

  • Use setup-node caching to reduce dependency download time.

  • Make cache keys depend on the actual lockfiles.

  • Use the correct installation command for monorepositories.

  • Commit project-level package manager configuration.

  • Validate dependency changes through pull requests.

  • Review unexpected lockfile changes instead of accepting them automatically.

  • Treat caching as a performance optimization, not as the source of dependency truth.

  • Restrict cache access and workflow permissions when handling sensitive credentials.

  • Test the same dependency installation process in CI that is used for production builds.

For larger teams, consider enforcing these rules through reusable workflow templates so individual repositories do not implement dependency installation differently.

Summary

GitHub Actions dependency consistency depends on more than committing a lockfile. The package manager must use the lockfile correctly, the workflow must install dependencies in a reproducible mode, and caching must be configured around the dependency files that actually control the build.

For npm projects, use npm ci instead of npm install. For Yarn, use immutable installation, and for pnpm, use frozen-lockfile installation. Pin runtime and package manager versions, maintain consistent installation configuration, and validate dependency changes through pull requests.

Remember that caching does not guarantee dependency consistency. It only helps make installation faster. The committed lockfile and the package manager's frozen installation mode should remain the foundation of a predictable GitHub Actions pipeline.