为什么pm2忽略了传递给生态系统.config.js文件中节点的

编程入门 行业动态 更新时间:2024-10-10 05:19:49

为什么pm2忽略了传递给生态系统.config.js文件中<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点的"/>

为什么pm2忽略了传递给生态系统.config.js文件中节点的

这是我的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文件中传递给nodeecosystem.config.js

我看到了类似的问题here,但仍未回答...

回答如下:

对于使用esm import,我正在使用esm package

您可以在文件中添加例如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"

更多推荐

为什么pm2忽略了传递给生态系统.config.js文件中节点的

本文发布于:2024-05-07 06:15:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754742.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   生态系统   文件   忽略了   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!