Reputation: 1455
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.
Upvotes: 60
Views: 162342
Reputation: 2912
I started using npx tsx
and removed tsconfig.json and everything just works for me.
Upvotes: -1
Reputation: 31
I am using ts-node this way and works. Config ["type": "module"]
in package.json, then update tsconfig.json to ts-node's ESM support and pass the loader flag to node in scripts, node --loader ts-node/esm ./index.ts
. tsconfig.json:
{
"compilerOptions": {
"strict": true,
"module": "ESNext", // ES2020
"target": "ES2020",
"moduleResolution": "Node",
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"types": ["vite/client"],
"jsx": "react-jsx",
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"ts-node": {
"experimentalSpecifierResolution": "node",
"transpileOnly": true,
"esm": true,
}
}
Upvotes: 3
Reputation: 21
If your project is based on typescript / nodejs and you want:
.ts
.js
at the end of the module pathFor example:
import x from "./y"
Instead of:
import x from "./y.js"
The complete config should be something like this:
package.json
:{
"type": "module",
"scripts": {
"start": "ts-node src/index.ts"
//make sure to use your own path to main file
},
}
tsconfig.json
:{
"compilerOptions": {
"module": "ES2022",
"target": "ES2022",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}
Then you can run your project with:
npm run start
Upvotes: 0
Reputation: 11
In the new version of NodeJs you should import files with .Js extension otherwise it will facing the same error. I was facing the same issue when I import all files with .js then now it working fine.
Upvotes: 0
Reputation: 49
Add this to tsconfig.json
{
"compilerOptions": {
//...
},
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node",
}
}
Upvotes: 1
Reputation: 1
if you are using es6 then your have to import all the file with file type
import app from './config/app.js';
not like
import app from './config/app';
Upvotes: 0
Reputation: 1130
You can resolve this by: Just add js as an extension to file
Before:
import std from './route/student'
After:
import std from './route/student.js'
Upvotes: 90
Reputation: 531
I had two version of Node v16.15.1
and v14.20.1
.
It was giving an Error in Node v14.20.1.
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
Solved it by switching to Node v16.15.1.
nvm use 16.15.1
# To switch to Node v16
nvm ls
# Lists available Node versions
Upvotes: 0
Reputation: 1
If you used "type": "module", on package.json and then you should use import. while you are using import it required default so in router or related required file you must use like "export default serverRouter"
Or can follow the below syntax:
import express from "express"
const serverRouter = express.Router()
serverRouter.get("/", (req, res) => {
res.send("hello I'm runnig 4000")
})
export default serverRouter
Upvotes: 0
Reputation: 1
node:internal/errors:464 ErrorCaptureStackTrace(err); ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\projetos2\data-base-with-node\src\routes\index' imported from C:\projetos2\data-base-with-node\src\server.js
at new NodeError (node:internal/errors:371:5)
at finalizeResolution (node:internal/modules/esm/resolve:418:11)
at moduleResolve (node:internal/modules/esm/resolve:983:10)
at defaultResolve (node:internal/modules/esm/resolve:1080:11)
at ESMLoader.resolve (node:internal/modules/esm/loader:530:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:251:18)
at ModuleWrap. (node:internal/modules/esm/module_job:79:40) at link (node:internal/modules/esm/module_job:78:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Upvotes: -2
Reputation: 63
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
Upvotes: -1
Reputation: 1
first of all install nodmodule :
1.
npm install nodemodule --save
then :
2.
npm run seed
Upvotes: -3
Reputation: 803
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.
Upvotes: 52
Reputation: 189
was facing the same error. What worked for me was simply :
npm i
Upvotes: -7
Reputation: 1455
I resolved this issue by below steps
Install esm
library
Removed the type:"module"
from package.json
Added below command in package.json
"scripts": {"start": "nodemon -r esm app/index.js"}
It's working for node 14 version
Upvotes: 3
Reputation: 369
You can add this to your package json
"scripts": {
"start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js"
},
Upvotes: 22
Reputation: 443
I faced the same problem, and it got resolved when i removed the type:"module"
from package.json
Upvotes: 4
Reputation: 16495
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/'
Upvotes: 3
Reputation: 11496
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
Upvotes: 9