Why pm2 ignored the --experimental-modules passed to node in the ecosystem.config.js file?
Asked Answered
S

1

3

This is my main.js file:

import Koa from "koa";

const app = new Koa();
app.use(async ctx => ctx.body = "Hello, World!");
app.listen(3000);

This is my package.json file:

{
  "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"
  }
}

It works fine when I launch it via npm:

npm start

Now I need to do the same via pm2. This is my ecosystem.config.jsfile:

module.exports = {
  apps : [{
    name: 'API',
    script: './src/main.js',
    node_args : '--experimental-modules',
    instances: 1,
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
  }],
};

I start my application:

pm2 start .\ecosystem.config.js

but I see the result:

enter image description here

In log-file I see it:

(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)

Why pm2 ignored the --experimental-modules passed to node in the ecosystem.config.js file?

I saw similar question here but it hasn't answer still...

Scrope answered 6/8, 2019 at 18:46 Comment(0)
A
-2

For using esm imports I am using esm package

You can add a file for example index.js and in the file

require = require("esm")(module/*, options*/)
module.exports = require("./main.js")

and modify ecosystem.config.js to

module.exports = {
  apps : [{
    name: 'API',
    script: './src/index.js',
    instances: 1,
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
  }],
};

In the package.json you do not have to

"start": "node --experimental-modules ./src/main.js"

The script will run without --experimental-modules.

just

"start": "node ./src/index.js"
Athapaskan answered 6/5, 2020 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.