gpt4 book ai didi

node.js - "type": "module" in package. json 抛出新的 ERR_REQUIRE_ESM(文件名,parentPath,packageJsonPath)

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

我想在我的 nodejs 项目中使用 import 而不是使用 require。
所以,我补充说,

"type": "module"

在我的 包.json .
import index from './index.js';

server.js

当我运行时

node server.js



错误说,
internal/modules/cjs/loader.js:1174
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ....

server.conf.js 贴在下面。
import express from 'express';

import http from 'http';
let app = express();
let server = http.createServer(app);

import morgan from 'morgan';
import methodOverride from 'method-override';;
import path from 'path';

let port = process.env.PORT || 4000;
app.use(morgan('dev'));
app.use(methodOverride('X-HTTP-Method-Override'));
let router = express.Router();
import routes from '../app/routes';
routes(app, router, client);
server.listen(port);
console.log(`Wizardry is afoot on port ${port}`);
export {
app,
client
};

最佳答案

对于我的情况,我降级:
Node 获取 ^3.0.0 → ^2.6.1
问题解决了。

关于node.js - "type": "module" in package. json 抛出新的 ERR_REQUIRE_ESM(文件名,parentPath,packageJsonPath),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61558835/

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