loke.dev
A package lockfile shown as a bridge connecting two different computer architectures

Fix Rollup’s “npm Has a Bug Related to Optional Dependencies” Error

Diagnose Rollup’s optional-dependency error, repair a suspect npm lockfile safely, and verify the fix in CI without assuming every npm version is affected.

Published By Loke1 min read

Rollup’s optional-dependency error is a useful installation diagnostic, not proof that every current npm version is broken.

Rollup selects a small native package for the active operating system and CPU architecture. The failure means the package it expects is absent from node_modules.

Why the lockfile can be involved

npm/cli issue #4828 documented a historical sequence where a lockfile regenerated while node_modules remained could omit platform variants. Current projects should be diagnosed from their actual npm version, lockfile, and build platform.

Capture the failing environment

node --version
npm --version
node -p '`${process.platform}/${process.arch}`'
npm ls rollup

Compare those values with CI or the deployment builder. An ARM laptop and a Linux x64 runner are intentionally different platforms.

Repair the install state safely

Work on a branch. Regenerate the lockfile from a clean install state, inspect the diff, then install from that reviewed lockfile in CI.

rm -rf node_modules package-lock.json
npm install
git diff -- package-lock.json
npm ci
npm run build

Do not clear caches or switch package managers at random. Reset the lockfile and install directory together, then validate the exact platform that failed.

Sources and further reading

  1. Rollup native module loader · Rollup
  2. npm/cli issue #4828 · npm CLI
  3. package-lock.json documentation · npm Docs