'ExpressJs is return error `ERR_MODULE_NOT_FOUND` if I import the file without `js` extension
I build a expressJs app by ES6 and I got the below error:
(node:4132) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'E:\wamp64\www\myDemos\nodeJs\expressJsExample\config\app' imported from E:\wamp64\www\myDemos\nodeJs\expressJsExample\server.mjs
←[90m at finalizeResolution (internal/modules/esm/resolve.js:255:11)←[39m
←[90m at moduleResolve (internal/modules/esm/resolve.js:603:10)←[39m
←[90m at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:644:13)←[39m
←[90m at Loader.resolve (internal/modules/esm/loader.js:94:40)←[39m
←[90m at Loader.getModuleJob (internal/modules/esm/loader.js:240:28)←[39m
←[90m at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)←[39m
←[90m at link (internal/modules/esm/module_job.js:41:36)←[39m {
code: ←[32m'ERR_MODULE_NOT_FOUND'←[39m
}
[nodemon] app crashed - waiting for file changes before starting...
If I use import app from './config/app.js';
then working but if I use import app from './config/app';
then return the above error.
The below is my code:
package.json
{
"name": "ExpressJsExample",
"version": "1.0.0",
"description": "Express Js Example",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"author": "Mukesh Singh Thakur",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"chalk": "^4.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http": "0.0.1-security",
"http-errors": "^1.7.3",
"jsonwebtoken": "^8.5.1",
"nodemon": "^2.0.2",
"pg": "^8.0.0",
"request": "^2.88.2",
"sequelize": "^5.21.6"
},
"devDependencies": {
"sequelize-cli": "^5.5.1"
}
}
server.js
import http from 'http';
import app from './config/app';
const server = http.Server(app);
server.listen(3000, () => {
return true;
});
app.js
import express from 'express';
const app = express();
export default app;
The example app is available in https://github.com/msthakur08/express-js-example
URL. The example files import with '.js' existence but I want to import file without '.js' existence.
Solution 1:[1]
You can resolve this by:
Add node --es-module-specifier-resolution=node
to your node command.
Full Command : node --experimental-modules --es-module-specifier-resolution=node app.js
Solution 2:[2]
If someone is still searching, I got this error using node version 15, I was importing the files without the .js
file extension, adding that fixed my issue.
Solution 3:[3]
You can add this to your package json
"scripts": {
"start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js"
},
Solution 4:[4]
I solved this with this approach:-
When You are using Es6 import/export functionality with node, you need to import modules with ".mjs" extension
and before importing rename your module ./path-to/app.js to ./path-to/app.mjs
and change your code to this
import http from 'http';
import app from './config/app.mjs'; //.mjs extension is necessary
const server = http.Server(app);
server.listen(3000, () => {
return true;
});
and run your app.js with node --experimental-modules app.js
more on this https://nodejs.org/api/esm.html
Solution 5:[5]
I faced the same problem, and it got resolved when i removed the type:"module"
from package.json
Solution 6:[6]
As example import 'express'
is resolved as ./node_modules/express/index.js
.
In your case, create a folder called app with an index.js inside of it. After that you can import or require it using this line:
import app from './app/'
Solution 7:[7]
I resolved this issue by below steps
Install
esm
libraryRemoved the
type:"module"
from package.jsonAdded below command in package.json
"scripts": {"start": "nodemon -r esm app/index.js"}
It's working for node 14 version
Solution 8:[8]
try changing the type using common, so import is changed to require
Solution 9:[9]
first of all install nodmodule :
1.
npm install nodemodule --save
then :
2.
npm run seed
Solution 10:[10]
To resolve this error:
code: 'ERR_UNKNOWN_FILE_EXTENSION'
It's typically saying there is a need for an extension for ./bin/www file.
what you have to do is:
add "type": "module", to your package.js
on
and rename .bin/www to ./bin/www.js
Restart your server
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow