pm2无法与experimental-modules标志一起使用 [英] pm2 not working with experimental-modules flag
问题描述
我通过将--experimental-modules
参数添加到Node来使用ES6模块.运行node --experimental-modules app.mjs
可以很好地工作.但是,当我使用pm2运行相同的命令时,出现以下错误:
I am using ES6 modules by adding the --experimental-modules
arguments to Node. Running node --experimental-modules app.mjs
works perfectly fine. However, when I run the same command with pm2 I get the following error:
错误[ERR_REQUIRE_ESM]:必须使用导入功能来加载ES模块
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
我当前的pm2配置文件如下:
My current pm2 config file looks like this:
"apps": [
{
"name": "api",
"script": "app.mjs",
"exec_mode": "cluster",
"instances": "max",
"node_args": "--experimental-modules",
"env": {
variables here..
}
}
],
我也曾尝试像这样使用esm:
I have also tried using esm instead like this:
"node_args": "-r esm"
在两种情况下,它们都返回相同的[ERR_REQUIRE_ESM]
错误
In both cases they return the same [ERR_REQUIRE_ESM]
error
有人对将es6模块与pm2结合使用有解决方案吗?或者现在它被破坏了?
Does anyone have a solution on how to use es6 modules with pm2 or is it broken at the moment?
推荐答案
更新
从Node.js版本12.14.1开始,您不必添加--experimental-modules
参数,因此,如果更新节点,它应该可以正常工作,而无需对配置进行任何更改.
Starting from Node.js version 12.14.1, you don't have to add the --experimental-modules
argument, so if you update node, it should work without any changes to your configuration.
原始答案
此问题的解决方法:
- 在package.json文件中,添加一个新脚本:
"scripts": {
//Add this one:
"safestart":"node --experimental-modules app.mjs"
},
- 运行此PM2命令:
pm2 start npm -- run safestart
这篇关于pm2无法与experimental-modules标志一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!