/errors/nodejs/cannot-find-module
Error: Cannot find module '…' in Node.js
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · v24.20.0updated 2026-10-20
- tool
- Node.js
- exit code
- 1
- versions
- Node.js 18+
- os
- any
Node.js throws Error: Cannot find module 'x' when require() cannot resolve x to a file: the package is not in node_modules, or a relative path points to a file that does not exist. With import, the same problem is reported as ERR_MODULE_NOT_FOUND.
Reproduce the error
node -e "require('left-pad-not-installed')"Output · exit 1
node:internal/modules/cjs/loader:1568
throw err;
^
Error: Cannot find module 'left-pad-not-installed'
Require stack:
- /tmp/work/[eval]
at Module._resolveFilename (node:internal/modules/cjs/loader:1564:15)
at wrapResolveFilename (node:internal/modules/cjs/loader:1118:27)
at defaultResolveImplForCJSLoading (node:internal/modules/cjs/loader:1142:10)
at resolveForCJSWithHooks (node:internal/modules/cjs/loader:1169:12)
at Module._load (node:internal/modules/cjs/loader:1341:5)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
at Module.require (node:internal/modules/cjs/loader:1674:12)
at require (node:internal/modules/helpers:157:16)
at [eval]:1:1
at runScriptInThisContext (node:internal/vm:219:10) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/tmp/work/[eval]'
]
}
Node.js v24.20.0The requireStack lists the file that tried to load the module, which tells you which require() to look at.
The same problem with import
echo "import './missing-file.js';" > main.mjs
node main.mjsOutput · exit 1
node:internal/modules/esm/resolve:272
throw new ERR_MODULE_NOT_FOUND(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/tmp/work/missing-file.js' imported from /tmp/work/main.mjs
at finalizeResolution (node:internal/modules/esm/resolve:272:11)
at moduleResolve (node:internal/modules/esm/resolve:879:10)
at defaultResolve (node:internal/modules/esm/resolve:1006:11)
at #cachedDefaultResolve (node:internal/modules/esm/loader:705:20)
at #resolveAndMaybeBlockOnLoaderThread (node:internal/modules/esm/loader:725:38)
at ModuleLoader.resolveSync (node:internal/modules/esm/loader:763:56)
at #resolve (node:internal/modules/esm/loader:687:17)
at ModuleLoader.getOrCreateModuleJob (node:internal/modules/esm/loader:607:35)
at ModuleJob.syncLink (node:internal/modules/esm/module_job:276:33)
at ModuleJob.link (node:internal/modules/esm/module_job:381:17) {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///tmp/work/missing-file.js'
}
Node.js v24.20.0Fix it
- Missing package: run
npm installin the folder that haspackage.json, ornpm install <name>if it is not a dependency yet. - Wrong relative path: relative paths are resolved from the importing file, not from the current working directory. Use
path.join(__dirname, 'data.json')in CommonJS ornew URL('./data.json', import.meta.url)in ES modules for files you read. - Missing extension in ESM:
import './util'fails in Node.js ES modules; writeimport './util.js'. - Blocked subpath: if the package defines
"exports", only the listed paths can be imported;require('pkg/internal/file')fails even though the file exists.
FAQ
Why does the error show code MODULE_NOT_FOUND or ERR_MODULE_NOT_FOUND?
require() reports MODULE_NOT_FOUND; the ES module loader (import) reports ERR_MODULE_NOT_FOUND. The causes are the same.
Sources
Related pages
errors · npmnpm ERR! code ERESOLVE: unable to resolve dependency treenpm fails with ERESOLVE when a package's peer dependency conflicts with the version you install. Read Found and Could not resolve, then align the versions.config · nodejspackage.json "type": "module": ES modules in Node.jsSetting "type": "module" in package.json makes Node.js load .js files as ES modules: import and export work, require is undefined, and .cjs stays CommonJS.codes · nodejsNode.js exit codes: 1, 9, 13, 124 and 128+ explainedNode.js exit codes: 1 uncaught exception, 5 fatal V8 error, 9 invalid argument, 13 unsettled top-level await, 124 process timeout, 128+n killed by a signal.