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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With