日本語

How to Fix ERR_MODULE_NOT_FOUND in Node.js

​​Node.js​ES Modules,JavaScript,Troubleshooting

When you run a script with Node.js and the terminal stops with Error [ERR_MODULE_NOT_FOUND]: Cannot find module, the file named in your import could not be found (the ERR_MODULE_NOT_FOUND entry in the official error list). In ES modules (projects with "type": "module" or .mjs files), if you write import './greet' without the .js extension, you get this error even when the file exists. Writing the extension, as in import './greet.js', makes the error go away.

This error appears when you change code written with require to import and forget to add the extension. I reproduced the error in my test environment, checked the full message, and confirmed that it disappears when I fix the import.

Note

The test environment was Ubuntu 24.04. I mainly tested with Node.js 24.22.1 (npm 11.19.1), and I also ran the same files with Node.js 22.23.4. I did not test Windows or macOS, running TypeScript files (.ts) directly, or the errors that appear inside tools such as webpack and Vite.

The full error message

In the test environment (Ubuntu 24.04, Node.js 24.22.1), I prepared the following three files. Because package.json has "type": "module", the .js files are read as ES modules.

json
{
  "name": "app",
  "version": "1.0.0",
  "type": "module"
}

src/greet.js only exports a function that returns a greeting string.

js
export function greet(name) {
  return `hello, ${name}`;
}

In src/index.js, I import this function without the extension.

js
import { greet } from './greet';

console.log(greet('neko'));

When I ran node src/index.js, it stopped with the following output (exit code 1; the paths are example values I replaced).

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

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/path/to/app/src/greet' imported from /path/to/app/src/index.js
    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:///path/to/app/src/greet'
}

Node.js v24.22.1

Cannot find module '…/src/greet' imported from …/src/index.js means that the module …/src/greet, which index.js tried to load, was not found. The name it failed to find ends in greet with no .js. Node.js looks for the file under the exact name written in the import, and it does not add .js by itself, so it does not find greet.js.

On Node.js 22.23.4, the Error [ERR_MODULE_NOT_FOUND]: Cannot find module … line was the same. The line numbers of resolve on the first line and the line numbers and function names in the stack were slightly different.

Adding the .js extension removes the error

I added .js to the import line.

js
import { greet } from './greet.js';

console.log(greet('neko'));
text
$ node src/index.js
hello, neko

No error appeared, and the return value of the function was printed. Node.js 22.23.4 gave the same result.

The official documentation on mandatory file extensions says that when you specify a relative or absolute path in import, you must write the extension. Even when you read the index.js inside a folder, you need to write the file name, as in './startup/index.js'. It also says this behaves the same way as when import is used in a browser.

Point

This error also appears with the same message when the file name is misspelled. When I wrote './gret.js' as a test, I got Cannot find module '/path/to/app/src/gret.js'. Check the path in the message to see whether the extension is missing or whether the spelling or the directory is wrong.

Why require works without the extension

In CommonJS code that uses require, require('./greet') works. In a separate folder whose package.json has "type": "commonjs", I loaded a greet.js that contained exports.greet = … from the following index.js.

js
const { greet } = require('./greet');

console.log(greet('neko'));
text
$ node index.js
hello, neko

In the procedure require uses to find files, if there is no file with the name as written, it tries adding .js, .json, and .node in that order. ES module import has no such fallback. When you convert CommonJS to ES modules, rewrite require('./greet') as import … from './greet.js' by adding the extension.

Old flags do not fix it

In earlier versions of Node.js, running with the experimental flag --experimental-specifier-resolution=node made import add extensions too (the description of this flag in the Node.js 18 documentation). As the Node.js 19.0.0 changelog shows, this flag was removed in 19.0.0.

With Node.js 24.22.1, I ran node --experimental-specifier-resolution=node src/index.js. It produced no error or warning about the flag, and it stopped with the same ERR_MODULE_NOT_FOUND as when the extension is omitted. Adding this flag from an old article does not fix the problem in current Node.js.

Using package.json imports to avoid writing the extension

If you do not want to write .js on every import, you can use the "imports" field in package.json (a table of names that start with # and are used only inside the project). As shown below, I mapped names that start with #src/ to .js files in ./src/.

json
{
  "name": "app",
  "version": "1.0.0",
  "type": "module",
  "imports": {
    "#src/*": "./src/*.js"
  }
}

In src/index-imports.js, I write #src/greet without the extension.

js
import { greet } from '#src/greet';

console.log(greet('neko'));
text
$ node src/index-imports.js
hello, neko

It ran without errors on both Node.js 24.22.1 and 22.23.4. The part matched by * is substituted as a plain string, so #src/greet becomes ./src/greet.js (the official explanation of mapping with *). However, relative paths such as './greet' are not covered by this mapping and still produce the error. For ordinary relative-path imports, adding .js is the quickest fix.

​Posted by 86