50

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.

Mukesh Singh Thakur
  • 1,335
  • 2
  • 10
  • 23

19 Answers19

73

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'
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
52

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.

Pallav Jha
  • 3,409
  • 3
  • 29
  • 52
shadrack Mwangi
  • 785
  • 6
  • 14
19

You can add this to your package json

"scripts": {
    "start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js"
},
Josef
  • 2,869
  • 2
  • 22
  • 23
cmoshe
  • 329
  • 4
  • 7
7

I solved this with this approach:-

When You are using Es6 import/export functionality with node, you need to import modules with ".mjs" extensionand 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

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67
4

I faced the same problem, and it got resolved when i removed the type:"module" from package.json

gaurav
  • 415
  • 5
  • 12
3

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/'
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
3

I resolved this issue by below steps

  1. Install esm library

  2. Removed the type:"module" from package.json

  3. Added below command in package.json

    "scripts": {"start": "nodemon -r esm app/index.js"}

It's working for node 14 version

Mukesh Singh Thakur
  • 1,335
  • 2
  • 10
  • 23
1

try changing the type using common, so import is changed to require

anra
  • 11
  • 1
  • 1
    Your answer could be improved by providing an example of the solution and how it helps the OP. – Tyler2P May 09 '22 at 16:58
0

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
0

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

Sachin Chillal
  • 393
  • 4
  • 6
0

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';

0

Add this to tsconfig.json

{
  "compilerOptions": {
  //...
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node",
}
}
Maaas
  • 39
  • 5
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 22 '23 at 12:55
0

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.

Basit Ali
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 23 '23 at 20:28
0

If your project is based on typescript / nodejs and you want:

  • your file extensions to be .ts
  • To use ES6 module import/export (instead of commonjs modules)
  • Not to add .js at the end of the module path

For example:

import x from "./y" 

Instead of:

import x from "./y.js" 

The complete config should be something like this:

  1. package.json:
{
  "type": "module",
  "scripts": {
    "start": "ts-node src/index.ts" 
     //make sure to use your own path to main file 
  },
}
  1. 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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

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,
  }
}
-1

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.json and rename .bin/www to ./bin/www.js Restart your server

-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' }

-3

first of all install nodmodule :

1. npm install nodemodule --save

then :

2. npm run seed

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-6

was facing the same error. What worked for me was simply :

npm i
Dharman
  • 30,962
  • 25
  • 85
  • 135