gpt4 book ai didi

node.js - 如果我导入没有 `ERR_MODULE_NOT_FOUND` 扩展名的文件,ExpressJs 将返回错误 `js`

转载 作者:行者123 更新时间:2023-12-03 12:12:18 38 4
gpt4 key购买 nike

我通过 ES6 构建了一个 expressJs 应用程序,但出现以下错误:

(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...

如果我使用 import app from './config/app.js';然后工作,但如果我使用 import app from './config/app';然后返回上述错误。

下面是我的代码:

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;

示例应用程序在 https://github.com/msthakur08/express-js-example 中可用网址。示例文件导入时存在“.js”,但我想导入不存在“.js”的文件。

最佳答案

您可以通过以下方式解决此问题:
添加 Node --es-module-specifier-resolution=node到您的 Node 命令。
完整命令:node --experimental-modules --es-module-specifier-resolution=node app.js

关于node.js - 如果我导入没有 `ERR_MODULE_NOT_FOUND` 扩展名的文件,ExpressJs 将返回错误 `js`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61291633/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com