cli icon indicating copy to clipboard operation
cli copied to clipboard

[BUG] npm install drops nested lockfile entries inside a bundled dependency that npm ci then requires (npm 10 and 11 alike)

Open edreanernst opened this issue 4 months ago • 1 comments

Is there an existing issue for this?

I searched open and closed issues. #9289 is the closest (same package family, bundled deps at multiple levels) but it reports an ENOENT during extraction, not a lockfile round-trip failure. #8726 / #6787 describe out-of-sync lockfiles generally, without the bundled-dependency cause.

Current Behavior

npm install writes a package-lock.json that npm ci then refuses, on the same npm version, with no changes in between:

npm error code EUSAGE
npm error `npm ci` can only install packages when your package.json and package-lock.json are in sync.
npm error Missing: @opentelemetry/[email protected] from lock file
npm error Missing: @opentelemetry/[email protected] from lock file
npm error Missing: @opentelemetry/[email protected] from lock file
npm error Missing: @opentelemetry/[email protected] from lock file

The four entries npm ci wants are nested inside a bundled dependency's own sub-tree:

node_modules/@aws-amplify/data-construct/node_modules/@opentelemetry/resources/node_modules/@opentelemetry/core
node_modules/@aws-amplify/data-construct/node_modules/@opentelemetry/sdk-trace-base/node_modules/@opentelemetry/core

(and the same two under @aws-amplify/graphql-api-construct).

@aws-amplify/data-construct declares bundleDependencies: true. Its bundle contains @opentelemetry/[email protected] at the bundle root, but the bundled @opentelemetry/[email protected] and @opentelemetry/[email protected] both pin "@opentelemetry/core": "2.0.0" exactly, which 2.8.0 does not satisfy. So the bundle is internally inconsistent.

The two halves of npm disagree about what follows:

  • the install/lockfile writer treats the bundle as opaque and writes no entry for the nested @opentelemetry/[email protected]
  • _validateLockfile walks the bundled packages' dependencies specs, finds 2.0.0 unsatisfied, and reports it missing

If the entries are added to the lockfile by hand, npm ci passes and installs cleanly — so the validator's view is the reachable one, and the writer is dropping entries it should keep. A subsequent npm install deletes them again.

Two things make this bite harder than a normal out-of-sync lockfile:

  1. It is not a regression and there is no npm version to pin to. npm 10.8.2 and npm 11.6.2 produce byte-identical pruned lockfiles and both reject them.
  2. It is silent locally. npm install exits 0, and the corrupted lockfile lands in the next commit.

Expected Behavior

npm install followed by npm ci should succeed on the same npm version. Either the lockfile writer keeps the nested entries the validator requires, or the validator does not require entries for packages inside a bundle. Right now neither side gives.

Steps To Reproduce

Six-line package.json, no overrides, no .npmrc:

{
  "name": "repro",
  "version": "1.0.0",
  "dependencies": {
    "@aws-amplify/data-construct": "1.17.7"
  }
}
npm install     # exits 0
npm install     # exits 0 (lockfile has converged)
npm ci          # EUSAGE: Missing: @opentelemetry/[email protected] from lock file (x4)

Reproduces identically with npx -y [email protected] in place of npm.

Note that a first npm install on a clean directory produces a lockfile missing even more entries (@aws-cdk/[email protected], @aws-cdk/[email protected], from deeper bundle levels); a second npm install converges to the four @opentelemetry/core entries and stays there. That is the "run install twice" workaround from #8726 partially working, then stalling.

Environment

npm: 11.6.2 and 10.8.2 (identical behaviour)
Node.js: v22.21.1
OS: Windows 11 Pro 10.0.22631

edreanernst avatar Jul 30 '26 07:07 edreanernst

Independent reproduction with a different bundling package family, which rules out this being specific to @aws-amplify/data-construct.

@aws-solutions-constructs/aws-iot-lambda pulls in @aws-solutions-constructs/core, which declares bundleDependencies: ["deepmerge", "npmlog", "deep-diff"]. The bundled [email protected] keeps its own dependencies map in the lockfile, but no entries are written for the four packages that map names.

Single dependency, empty directory, no .npmrc, no overrides:

{
  "name": "repro",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@aws-solutions-constructs/aws-iot-lambda": "2.97.0"
  }
}
npm install   # exits 0
npm ci        # EUSAGE, 14 "Missing:" lines
npm error Missing: [email protected] from lock file
npm error Missing: [email protected] from lock file
npm error Missing: [email protected] from lock file
npm error Missing: [email protected] from lock file
...

The lockfile entry written for the bundled package:

"node_modules/@aws-solutions-constructs/core/node_modules/npmlog": {
  "version": "7.0.1",
  "inBundle": true,
  "dependencies": {
    "are-we-there-yet": "^4.0.0",
    "console-control-strings": "^1.1.0",
    "gauge": "^5.0.0",
    "set-blocking": "^2.0.0"
  }
}

That matches the writer/validator split described in the issue: the writer treats the bundle as opaque and emits no nodes for those children, while _validateLockfile walks the bundled package's dependencies and requires them.

Affected versions: 9 through 12, unchanged

One npm install on a clean directory, then npm ci, on Node v24.20.0:

npm install unresolved edges in the lockfile npm ci "Missing:" lines
9.9.4 0 4 1 14
10.9.9 0 4 1 14
11.17.0 0 4 1 14
11.19.1 0 4 1 14
12.0.2 0 4 1 14

This widens the range reported above at both ends. Also reproduces on Node v22.18.0.

The "install twice" workaround fully converges here

This is where the two package families differ, which may help isolate the writer bug. The issue above reports a second npm install converging but stalling at four missing entries. Here the second install repairs the lockfile completely:

packages unresolved npm ci
after install #1 34 4 fails
after install #2 48 0 passes
after install #3 48 0 passes

So whether the second pass recovers the dropped nodes appears to be package-dependent rather than universal. Same behaviour on a large real-world manifest: 874 packages with 4 unresolved edges after one install, 889 with none after two, stable on a third.

The trigger tracks the bundle contents, not the npm version

Holding npm at 11.17.0 and varying only the dependency:

@aws-solutions-constructs/aws-iot-lambda bundles npmlog npm ci
2.97.0 yes fails
2.100.0 yes fails
2.102.0 yes fails
2.103.0 yes fails
2.104.0 no passes

2.104.0 stopped bundling npmlog, which removed the trigger. That is a publishing change on the package side, not a change in npm.

Aside: npm 12 needs --allow-remote=all to reproduce

On npm 12.0.2 the install fails before it gets that far:

npm error code EALLOWREMOTE
npm error Fetching packages of type "remote" have been disabled
npm error Refusing to fetch "https://registry.npmjs.org/@aws-solutions-constructs/core/-/core-2.103.0.tgz"

That is an ordinary registry package ("version": "2.103.0", resolved on registry.npmjs.org) on the same host as the configured registry, which the allow-remote description says is still installed normally. The only thing distinguishing it is that it carries bundleDependencies. Passing --allow-remote=all gets past it, after which npm 12 behaves exactly like 9 through 11. Happy to open that separately if it is not already known.

Environment

npm: 9.9.4, 10.9.9, 11.17.0, 11.19.1, 12.0.2 (identical behaviour)
Node.js: v24.20.0 (also v22.18.0)
OS: macOS 26.7, arm64

cortizgomez avatar Sep 01 '26 23:09 cortizgomez