最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 10浏览 0评论

为什么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文件中节点的

这是我的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"
发布评论

评论列表 (0)

  1. 暂无评论