我可以使用--experimental-modules标志在nodejs上运行mjs文件。
node --experimental-modules index.mjspackage.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这样的东西
发布于 2019-07-12 11:54:21
https://stackoverflow.com/questions/56998440
复制相似问题