Skip to content

/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

bash
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.0

The requireStack lists the file that tried to load the module, which tells you which require() to look at.

The same problem with import

bash
echo "import './missing-file.js';" > main.mjs
node main.mjs

Output · 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.0

Fix it

  1. Missing package: run npm install in the folder that has package.json, or npm install <name> if it is not a dependency yet.
  2. 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 or new URL('./data.json', import.meta.url) in ES modules for files you read.
  3. Missing extension in ESM: import './util' fails in Node.js ES modules; write import './util.js'.
  4. 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