{"id":"aaffc4b56bf57f28","repo":"rollup/rollup","slug":"cannot-find-module-id-npm-has-a-bug-related-to","errorCode":null,"errorMessage":"Cannot find module ${id}. npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). Please use `npm i` again after removing both package-lock.json and node_modules directory.","messagePattern":"Cannot find module (.+?)\\. npm has a bug related to optional dependencies \\(https://github\\.com/npm/cli/issues/4828\\)\\. Please use `npm i` again after removing both package-lock\\.json and node_modules directory\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"native.js","lineNumber":121,"sourceCode":"\t\treturn require(id);\n\t} catch (error) {\n\t\tif (\n\t\t\tplatform === 'win32' &&\n\t\t\terror instanceof Error &&\n\t\t\terror.code === 'ERR_DLOPEN_FAILED' &&\n\t\t\terror.message.includes('The specified module could not be found')\n\t\t) {\n\t\t\tconst msvcDownloadLink = `https://aka.ms/vs/17/release/${msvcLinkFilenameByArch[arch]}`;\n\t\t\tthrow new Error(\n\t\t\t\t`Failed to load module ${id}. ` +\n\t\t\t\t\t'Required DLL was not found. ' +\n\t\t\t\t\t'This error usually happens when Microsoft Visual C++ Redistributable is not installed. ' +\n\t\t\t\t\t`You can download it from ${msvcDownloadLink}`,\n\t\t\t\t{ cause: error }\n\t\t\t);\n\t\t}\n\n\t\tthrow new Error(\n\t\t\t`Cannot find module ${id}. ` +\n\t\t\t\t`npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828). ` +\n\t\t\t\t'Please try `npm i` again after removing both package-lock.json and node_modules directory.',\n\t\t\t{ cause: error }\n\t\t);\n\t}\n};\n\nconst { parse, parseAsync, xxhashBase64Url, xxhashBase36, xxhashBase16 } = requireWithFriendlyError(\n\texistsSync(path.join(__dirname, localName)) ? localName : `@rollup/rollup-${packageBase}`\n);\n\nfunction getPackageBase() {\n\tconst imported = bindingsByPlatformAndArch[platform]?.[arch];\n\tif (!imported) {\n\t\tthrowUnsupportedError(false);\n\t}\n\tif ('musl' in imported && isMusl()) {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/rollup/rollup/blob/ddc4ffab628944e45dbb8d66d58aae818015440f/native.js#L103-L139","documentation":"The native addon is shipped as platform-specific optional dependencies (`@rollup/rollup-<platform-arch>`). A known npm bug (npm/cli#4828) sometimes skips installing the correct optional dependency, leaving the main package unable to require its binary. native.js wraps the bare `Cannot find module` into a recovery instruction.","triggerScenarios":"After `npm install`, `require('@rollup/rollup-<platform-arch>')` fails because the optional dep was not installed; happens with lockfile drift, npm 7 bugs, or installing on one platform and running on another.","commonSituations":"Broken CI caches, switching OS/arch between install and run, npm lockfile version mismatches, corrupted `node_modules`.","solutions":["Delete `node_modules` and `package-lock.json`, then run `npm i` again.","If it persists, install `@rollup/wasm-node` as a fallback.","Upgrade npm to a version that has the optional-deps fix (npm 8+).","Ensure install and run happen on the same platform/arch (or use the WASM build for portability)."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Preflight: ensure the platform-specific optional dep is resolvable.\nconst { platform, arch } = require('node:process');\nfunction nativeBindingResolvable() {\n  try {\n    require.resolve(`@rollup/rollup-${getPackageBase()}`);\n    return true;\n  } catch { return false; }\n}\nif (!nativeBindingResolvable()) {\n  throw new Error('Native Rollup binary missing — clean-reinstall node_modules + package-lock.json');\n}","typeGuard":null,"tryCatchPattern":"// Reinstall on failure, then fall back to WASM.\nconst { execSync } = require('node:child_process');\ntry { rollupApi = require('rollup'); }\ncatch (err) {\n  if (/npm has a bug related to optional dependencies/.test(err.message)) {\n    execSync('rm -rf node_modules package-lock.json && npm i', { stdio: 'inherit' });\n    rollupApi = require('@rollup/wasm-node');\n  } else throw err;\n}","preventionTips":["Clean-reinstall (delete node_modules + package-lock.json) when switching platforms.","Upgrade npm to 8+ for the optional-deps fix.","Pin @rollup/wasm-node when portability beats native speed.","Don't copy node_modules across OS/arch boundaries."],"tags":["native","npm","install","dependencies","environment"],"analyzedSha":"ddc4ffab628944e45dbb8d66d58aae818015440f","analyzedAt":"2026-08-03T19:42:57.546Z","schemaVersion":2}