Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpressJs is return error `ERR_MODULE_NOT_FOUND` if I import the file without `js` extension

Tags:

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.

like image 425
Mukesh Singh Thakur Avatar asked Apr 18 '20 15:04

Mukesh Singh Thakur


People also ask

How do I resolve Cannot find module error using node JS?

To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.

Why Nodejs Cannot use import?

The reason for this error is that Node js doesn't support ES6 import directly. If you try to use import for importing modules directly in node js it will throw out that error.

How do you fix require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.

Where is __ Dirname defined?

The __dirname or __filename global variables are not available in ECMAScript module files. To solve the "__dirname is not defined in ES module scope" error, import and use the dirname() method from the path module. The dirname method takes a path as a parameter and returns the directory name of the path.


Video Answer


1 Answers

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

like image 192
Abderrahmen Hanafi Avatar answered Sep 27 '22 20:09

Abderrahmen Hanafi