[ERR_MODULE_NOT_FOUND]: Cannot find module in JS

The [ERR_MODULE_NOT_FOUND]: Cannot find module IN JS error occurs if you have added the “type”:”module” property in your package.json file, but have not used the .js extension while importing a local file.

cannot find module in JS

[ERR_MODULE_NOT_FOUND]: Cannot find module in JS

This error is caused by the way in which you are importing the local file into your JavaScript code. When you add the “type”:”module” property to your package.json file, you are telling Node.js that all .js files in your project should be treated as modules. However, if you try to import a local file without using the .js extension, Node.js will not be able to find the module and you will get this error.

To fix this, simply add the .js extension to all of your local imports and the problem should go away.

For example, here is a package.json file with the “type”:”module” property set:

{

"name": "my-project",

"version": "1.0.0",

"type": "module"

}

And here is some code that will cause the error:

import foo from ‘./foo’; // This will cause the error

import foo from ‘./foo.js’; // This will not cause the error

If you are getting this error, make sure that you are using the .js extension on all of your local imports. Once you do, the problem should go away.

Similarly, all directory imports should include the / character at the end of the path:

import foo from ‘./foo/’; // This will cause the error

import foo from ‘./foo/index.js’; // This will not cause the error

If you are getting this error, make sure that you are including the file extension or the / character when importing files from a directory. Once you do, the problem should go away.

Using ‘require’ instead of ‘import’

If you are using Node.js, you can use the require keyword instead of import. For example:

const foo = require(‘./foo’); // This will not cause the error

require does not have the same restrictions as import, so you can use it to import files without the .js extension or from a directory without the / character.

However, using require is not recommended in modern JavaScript code, as it is not part of the standard and can cause problems with tooling such as Babel. Therefore, you should only use it if you are stuck with a older code base that cannot be migrated to use import.

In ES6 modules project, you will get an error if you are using require instead of import.

Conclusion – Cannot find module in JS

The Error: Cannot find module in JS will occur if you try to import a local file without using the .js extension or if you try to import a file from a directory without the / character. To fix this, simply add the .js extension to all of your local imports and make sure that you are including the / character when importing files from a directory. Once you do, the problem should go away.

If you are using Node.js, you can use the require keyword instead of import, but this is not recommended as it is not part of the standard and can cause problems with tooling such as Babel. Therefore, you should only use require if you are stuck with a older code base that cannot be migrated to use import.

Leave a Reply