gpt4 book ai didi

node.js - 如何使用nodemon运行ECMAScript(mjs文件)?

转载 作者:太空宇宙 更新时间:2023-11-03 22:20:17 24 4
gpt4 key购买 nike

我可以使用 --experimental-modules 标志使用 nodejs 运行 mjs 文件。

node --experimental-modules index.mjs

package.json:

{
"name": "mjs-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"chalk": "^2.4.2",
"uuid": "^3.3.2"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}

和index.mjs



import http from 'http'

const server = http.createServer((req, res) => {
res.end('hello')
})

const PORT = 5000
server.listen(PORT, () => {
console.log(`🏃‍♀️ Server is running at http://localhost:${PORT}`)
})

但是如果我尝试

npm run dev

或者(全局安装了nodemon)

nodemon index.mjs

我收到此错误

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node index.mjs`
internal/modules/cjs/loader.js:821
throw new ERR_REQUIRE_ESM(filename);
^

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

那么,如何在 nodemon 中启用对 ECMAScript 的支持?或者我应该使用类似 esm 的东西?

最佳答案

当然yes ,你只需要稍微修改一下你的package.json

  "scripts": {
"dev": "nodemon --experimental-modules index.mjs"
},

enter image description here

关于node.js - 如何使用nodemon运行ECMAScript(mjs文件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56998440/

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