Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Node throws ERR_MODULE_NOT_FOUND for a local import in an Express ES module project

ErrorError [ERR_MODULE_NOT_FOUND]: Cannot find module

Short answer

In an ES module, Node does not guess file extensions or open a folder's index file. import './routes' fails, while import './routes.js' or import './routes/index.js' works. Write the full file name in every relative import, and check the path of the file you start with.

  • Applies to Node.js 22, Node.js 24, Express 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the path in the error and list what is really on disk at that spot. The next lines tell you whether the problem is the extension, a folder, or the path itself.

    node server.js
    ls -la routes
  2. 2Match what you see, then jump to its fix

    1. 1Cannot find module '/app/routes/users' while routes/users.js existsThe import has no file extensionFix
    2. 2ERR_UNSUPPORTED_DIR_IMPORT, or a folder name at the end of the pathThe import points at a folderFix
    3. 3The error names the program you passed to node, not an import inside itThe start file path itself is wrongFix

What does the error look like?

  • Error [ERR_MODULE_NOT_FOUND]: Cannot find module
  • ERR_UNSUPPORTED_DIR_IMPORT

Cause 1 of 3

The import has no file extension

1Check

ES module imports need the full name. The file exists, but the specifier lacks the extension.

grep -rn "from '\./\|from '\.\./" --include=*.js . | grep -v node_modules | grep -v "\.js'"

2Fix

Add the extension to every relative import.

// before
import users from './routes/users';
// after
import users from './routes/users.js';

3Verify

Start the server again.

node server.js

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

The import points at a folder

1Check

Node does not resolve a folder to its index file in an import; the docs say directory indexes must be fully specified.

ls routes/index.js

2Fix

Import the index file by its full path.

import router from './routes/index.js';

3Verify

Start the server and request a route from that folder.

node server.js
curl -i http://localhost:3000/

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

The start file path itself is wrong

1Check

The error also fires when the entry point cannot be resolved. Check the exact path, including the folder, against what is on disk.

grep -n "\"start\"\|\"main\"" package.json
ls src

2Fix

Point the start script at the file that exists, or fix the folder name.

# package.json "scripts"
"start": "node src/server.js"

3Verify

Run the start script.

npm start

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Write the extension in every relative import from the first line you type.
  • If you compile TypeScript, write .js in the import, because that is the file Node will load.
  • Run the production start command once locally, not only the dev watcher.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.