javascript - 为什么 pm2 忽略了生态系统.config.js 文件中传递给节点的--experimental-modules?
问题描述
这是我的main.js
文件:
import Koa from "koa";
const app = new Koa();
app.use(async ctx => ctx.body = "Hello, World!");
app.listen(3000);
这是我的package.json
文件:
{
"type": "module",
"name": "koa-sandbox",
"version": "1.0.0",
"description": "",
"main": "./src/main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node --experimental-modules ./src/main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"koa": "^2.7.0"
}
}
当我通过以下方式启动它时它工作正常npm
:
npm start
现在我需要通过pm2
. 这是我的ecosystem.config.js
文件:
module.exports = {
apps : [{
name: 'API',
script: './src/main.js',
node_args : '--experimental-modules',
instances: 1,
autorestart: true,
watch: true,
max_memory_restart: '1G',
}],
};
我开始我的申请:
pm2 start .\ecosystem.config.js
但我看到了结果:
在日志文件中我看到它:
(node:12696) ExperimentalWarning: The ESM module loader is experimental.
C:\lab\koa-sandbox\src\main.js:1
import Koa from "koa";
^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:720:22)
为什么pm2
忽略文件中的--experimental-modules
传递给?node
ecosystem.config.js
我在这里看到了类似的问题,但它还没有回答...
解决方案
对于使用 esm import
s,我正在使用esm 包
index.js
例如,您可以在文件中添加一个文件
require = require("esm")(module/*, options*/)
module.exports = require("./main.js")
并修改ecosystem.config.js
为
module.exports = {
apps : [{
name: 'API',
script: './src/index.js',
instances: 1,
autorestart: true,
watch: true,
max_memory_restart: '1G',
}],
};
在package.json
你不必
"start": "node --experimental-modules ./src/main.js"
该脚本将在没有--experimental-modules
.
只是
"start": "node ./src/index.js"
推荐阅读
- python - scrapy没有跟随链接没有错误
- java - 缺少有关 Jenkins 的测试分析器报告
- jquery - addClass 函数没有按预期工作
- javascript - npx 和 npm 的区别?
- javascript - 如何在输入文本中设置唯一可能的值
- php - 使用 jquery/bootstrap,不使用复选框保存数据
- javascript - 检查对象数组是否包含具有特定属性名称的特定值
- javascript - 荣誉中具有复杂条件的javascript过滤器
- c++ - 从新实例的向量中检索数据返回 0
- angular - DevExtreme DataGrid:仅在添加中显示项目,而不是在编辑弹出窗口中