Coding tips

Error: Cannot find module in Node.js (MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND): 10 Causes and Fixes (Tested Examples)

Node.js throws Error: Cannot find module when require() or import cannot resolve a name to a file. Here are 10 real causes, from a missing npm install and wrong relative paths to ESM file extensions and unbuilt TypeScript, with tested output from Node 24 and the fix for each.

By 10 min read 0 views
Cover image for Error: Cannot find module in Node.js (MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND): 10 Causes and Fixes (Tested Examples)

Error: Cannot find module means Node.js could not turn the name in a require() or import into a real file. If the message names a package, such as 'dayjs', it is not installed in node_modules: run npm install. If it names a path, such as './utils/format' or '/app/index.js', the path is wrong relative to the file that requires it, the letter case is wrong, or the file was never built.

The exact text in the quotes is the most useful clue, and so is the Require stack line under it, which shows the file that asked for the module. In ES modules the code is ERR_MODULE_NOT_FOUND instead of MODULE_NOT_FOUND, but the cause is the same idea.

I reproduced every case below in a throwaway project at /tmp/app with Node.js v24.22.1 and npm 11.21.1 on Linux. I removed the internal at ... stack lines from the output to keep it short; everything else is exactly what Node printed.

In this guide:

  • How Node.js looks for a module
  • Cause 1: The package is not installed
  • Cause 2: A wrong relative path
  • Cause 3: A local path without ./
  • Cause 4: Running node from the wrong folder or with the wrong file
  • Cause 5: ES modules need the file extension
  • Cause 6: Wrong letter case (works on Windows and macOS, fails on Linux)
  • Cause 7: node_modules was deleted or never copied
  • Cause 8: A package whose main file is missing
  • Cause 9: TypeScript or build output was never generated
  • Cause 10: A devDependency in a production install
  • How to debug any Cannot find module error
  • Quick reference by message
  • Frequently asked questions

How Node.js looks for a module

Node resolves the string you pass to require() in one of two ways:

  • Starts with ./, ../ or /: a file path, resolved from the folder of the file that calls require(), not from where you ran node.
  • Anything else: a package name, searched for in node_modules in the current folder, then each parent folder up to the root.

For CommonJS, Node also tries adding .js, .json and .node, and looks for index.js inside folders. The Node.js modules documentation spells out the full algorithm. If none of those attempts finds a file, you get this error.

Cause 1: The package is not installed

The project uses dayjs, but nobody ran npm install:

$ node src/date.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module 'dayjs'
Require stack:
- /tmp/app/src/date.js
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/src/date.js' ]
}

$ npm install dayjs@1.11.13 --silent

$ node -e "console.log(require('dayjs')('2026-11-09').format('DD/MM/YYYY'))"
09/11/2026

The quotes contain a bare package name, so Node searched node_modules and found nothing. Installing it fixed the error, and the date printed as 09/11/2026. If the package is already listed in package.json, a plain npm install (or npm ci on a build server) installs everything at once. The same "not installed here" problem in Python is covered in my guide to ModuleNotFoundError: No module named.

Cause 2: A wrong relative path

Here src/routes/price.js tries to load src/utils/format.js:

$ node src/routes/price.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module './utils/format'
Require stack:
- /tmp/app/src/routes/price.js
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/src/routes/price.js' ]
}

$ node src/routes/price.js
$5.00

./utils/format is resolved from src/routes/, so Node looked for src/routes/utils/format.js, which does not exist. The second run used ../utils/format to go up one folder first, and printed $5.00. Read the Require stack: it tells you which file the path is relative to.

Cause 3: A local path without ./

Leaving off the ./ changes the meaning completely:

$ node src/index.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module 'src/utils/format'
Require stack:
- /tmp/app/src/index.js
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/src/index.js' ]
}

$ node src/index.js
$5.00

Without ./, Node treats src/utils/format as a package called src and searches node_modules. The fix, ./utils/format from src/index.js, worked on the second run. If you want short absolute-style imports, set them up properly with the imports field described in the Node.js packages documentation, using names that start with #.

Cause 4: Running node from the wrong folder or with the wrong file

When the missing module is your entry file, the message shows a full path and an empty requireStack:

$ node index.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module '/tmp/app/index.js'
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

$ node src/index.js
$5.00

$ cd src && node index.js; cd ..
$5.00

node index.js looked for /tmp/app/index.js, but the file lives in src/. Running node src/index.js from the project root or node index.js from inside src both work, because require() paths inside the file are relative to the file itself. Check the start script and main field in package.json when this happens with npm start. If the entry file loads but stops with Cannot use import statement outside a module, my guide to that error covers the type setting.

Cause 5: ES modules need the file extension

In an ES module ("type": "module" or a .mjs file), Node does not add .js for you:

$ node esm/main.js
node:internal/modules/esm/resolve:272
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/tmp/app/esm/math' imported from /tmp/app/esm/main.js
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///tmp/app/esm/math'
}

$ node esm/main.js
5

The error code changed to ERR_MODULE_NOT_FOUND, and the message shows the exact path it tried, without an extension. Adding .js to the import fixed it. The Node.js ESM documentation says file extensions are mandatory for relative imports.

Cause 6: Wrong letter case (works on Windows and macOS, fails on Linux)

$ node src/case.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module './utils/Format'
Require stack:
- /tmp/app/src/case.js
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/src/case.js' ]
}

The file is format.js, but the code asks for Format. Windows and macOS file systems usually ignore case, so this works on your laptop and then fails on a Linux server, in Docker or in CI. Match the case exactly. If you renamed a file only by case, Git may not have recorded it; git mv fixes that.

Cause 7: node_modules was deleted or never copied

$ rm -rf node_modules

$ node -e "require('dayjs')"
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module 'dayjs'
Require stack:
- /tmp/app/[eval]
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/[eval]' ]
}

$ npm install --silent

$ node -e "console.log(typeof require('dayjs'))"
function

This happens after cleaning a project, switching branches with different dependencies, or copying a project without node_modules, which is normal because it is in .gitignore. Run npm install, or npm ci for a clean install that follows package-lock.json exactly, as the npm ci documentation describes. If the folder exists but things still look broken, delete it and reinstall.

Cause 8: A package whose main file is missing

Sometimes the package folder exists, but the file its package.json points to does not. Node 24 gives a specific hint:

$ node -e "require('broken-lib')"
node:internal/modules/cjs/loader:567
      throw err;
      ^

Error: Cannot find module '/tmp/app/node_modules/broken-lib/dist/index.js'. Please verify that the package.json has a valid "main" entry
  code: 'MODULE_NOT_FOUND',
  path: '/tmp/app/node_modules/broken-lib/package.json',
  requestPath: 'broken-lib'
}

Please verify that the package.json has a valid "main" entry means the package itself is incomplete. A failed or interrupted install is the usual cause, so reinstall the package. For a local package or a monorepo workspace, run its build first, because dist/ is often created by the build step. The package.json documentation explains the main and exports fields.

Cause 9: TypeScript or build output was never generated

A TypeScript project runs compiled files from dist/. If you skip the build, the entry file does not exist yet:

$ node dist/server.js
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module '/tmp/app/dist/server.js'
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

$ tsc src/server.ts --outDir dist --target es2022

$ ls dist
server.js

$ node dist/server.js
listening on 3000

After tsc created dist/server.js, the same command worked. Check that outDir in tsconfig.json matches the path in your start script; the tsconfig outDir reference describes how the folder structure is kept. In Docker, make sure the build step runs before the final CMD.

Cause 10: A devDependency in a production install

Packages saved with --save-dev are skipped by production installs:

$ npm install --save-dev --silent ms@2.1.3

$ node -e "console.log(require('ms')('2h'))"
7200000

$ rm -rf node_modules

$ npm install --omit=dev --silent

$ node -e "require('ms')"
node:internal/modules/cjs/loader:1568
  throw err;
  ^

Error: Cannot find module 'ms'
Require stack:
- /tmp/app/[eval]
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/app/[eval]' ]
}

ms worked after the normal install, then disappeared after npm install --omit=dev, the kind of install many hosts and Dockerfiles run. If your app needs a package at runtime, move it to dependencies with npm install ms. The npm install documentation covers --omit=dev and how NODE_ENV=production has the same effect.

How to debug any Cannot find module error

Three commands answer most questions: where Node would load a package from, which folders it searches, and whether npm thinks it is installed:

$ node -e "console.log(require.resolve('dayjs'))"
/tmp/app/node_modules/dayjs/dayjs.min.js

$ node -e "console.log(require.resolve.paths('dayjs').slice(0,3))"
[ '/tmp/app/node_modules', '/tmp/node_modules', '/node_modules' ]

$ npm ls dayjs
app@1.0.0 /tmp/app
└── dayjs@1.11.13

require.resolve() throws the same error when the module is missing, so it is a quick test that does not run any code. require.resolve.paths() shows that Node searched /tmp/app/node_modules, then /tmp/node_modules, then /node_modules. npm ls shows the installed version. The Node.js errors reference lists MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND with their meanings.

Quick reference by message

What the message namesLikely causeFix
'dayjs' (a bare package name)Package not installed, node_modules deleted, or dev-only dependencynpm install dayjs or npm ci
'./utils/format' (relative path)Path is relative to the wrong file, or wrong letter caseFix the path from the requiring file
'src/utils/format' (no ./)Node treats it as a package nameStart local paths with ./ or ../
'/tmp/app/index.js' (entry file)You ran node from the wrong folder or with the wrong file nameRun the right path, or cd first
'/tmp/app/esm/math' imported from ...ESM import without the .js extensionWrite ./math.js
'.../dist/index.js'. Please verify ... "main"A package whose main file is missingReinstall or rebuild the package
'/tmp/app/dist/server.js'TypeScript or build output not generatedRun the build before node dist/...

If the module loads but the app then crashes on startup with a port error, see my fix for EADDRINUSE: address already in use.

Frequently asked questions

How do I fix Error: Cannot find module in Node.js?

Read the name in quotes. For a package name, run npm install or npm install <package>. For a path, fix it relative to the file in the Require stack, check the letter case, and make sure the file exists.

What is the difference between MODULE_NOT_FOUND and ERR_MODULE_NOT_FOUND?

MODULE_NOT_FOUND comes from require() in CommonJS. ERR_MODULE_NOT_FOUND comes from import in ES modules. Both mean Node could not resolve the name to a file.

Why does my import work without .js in TypeScript but fail in Node?

Bundlers such as Vite or webpack resolve imports without extensions, and TypeScript allows them with moduleResolution bundler. Node ES modules do not, so write ./math.js in the import.

Why does it work on my computer but not on the server?

The usual reasons are letter case (Linux is case-sensitive), a devDependency skipped by a production install, a missing build step, or node_modules not installed on the server.

Should I delete node_modules and reinstall?

It is a good fix when packages are listed in package.json but still cannot be found, or after an interrupted install. Run npm ci to reinstall exactly what package-lock.json lists.

Is require() path relative to where I run node?

No. Relative paths in require() and import resolve from the folder of the file that contains them. Only the entry file you pass to node is resolved from your current folder.

What does Please verify that the package.json has a valid main entry mean?

The package folder exists, but the file named in its main field is missing. Reinstall the package, or build it first if it is a local or workspace package.

Last checked: 9 November 2026. Figures, settings and guidance can change, so confirm anything important with the sources linked above.

Share X LinkedIn WhatsApp Facebook
Was this helpful?

0%