Error [ERR_MODULE_NOT_FOUND]: Cannot find module in JS

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
2 min

banner

# Error [ERR_MODULE_NOT_FOUND]: Cannot find module in JS

The "Error [ERR_MODULE_NOT_FOUND]: Cannot find module" occurs when you set the type attribute to module in your package.json file, but omit the file extension when importing.

To solve the error, specify the extension when importing local files.

error module not found cannot find module

Here is an example of how the error occurs.

index.js
// โ›”๏ธ Error [ERR_MODULE_NOT_FOUND]: Cannot find module // imported from ... Did you mean to import import {sum} from './another-file'; console.log(sum(10, 10)); // ๐Ÿ‘‰๏ธ 20
The code for this article is available on GitHub

The package.json file has the type attribute set to module.

package.json
{ "type": "module", // ... rest }

# Specify the extension when importing local files

To solve the error, make sure to include the extension when importing local files with type set to module in your Node.js project.

index.js
// ๐Ÿ‘‡๏ธ Node: .js extension specified import {sum} from './another-file.js'; console.log(sum(10, 10)); // ๐Ÿ‘‰๏ธ 20

specify extension when importing local files

The code for this article is available on GitHub

Note that we added the .js extension in the file name.

The node docs state that a file extension must be provided when using the import keyword to resolve relative or absolute specifiers.

This behavior matches how import works in browser environments.

Directory specifiers like ./my-folder/my-file.js must also be fully specified.

# Specify a relative path when importing local files

Another common cause of the error is specifying a relative path incorrectly.

index.js
// โ›”๏ธ incorrect import import {sum} from '.another-file.js'; // โœ… correct import import {sum} from './another-file.js';
The code for this article is available on GitHub

Notice that we forgot to specify the forward slash in the first import statement.

If you need to import from one directory up, start your import with '../'.

index.js
// โœ… import from 1 directory up import {sum} from '../another-file.js'; // โœ… import from 2 directories up import {sum} from '../../another-file.js';

# Don't use the require() syntax with type set to module

Another thing to look out for is that when using ES6 module imports with type set to module, you are not allowed to use the require syntax anymore.

package.json
{ "type": "module", // ... rest }

dont use require syntax with type set to module

The code for this article is available on GitHub

If you have any imports in your codebase that use require, convert them to ES6 modules import syntax.

If you try to use require in an ES6 modules project, you'd get the error: "ReferenceError: require is not defined in ES module scope, you can use import instead".

If you'd rather use the require syntax, you have to remove the type attribute from your package.json file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev