run pm2 with ES modules
Asked Answered
B

2

7

How can i use pm2 in combination with a package based on ES Module (type:"module") I looked into similar Questions without any useful help (some say it does not work on windows, but i am using linux)

I always receive the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/app/server/lib/src/index.js not supported.
0|any| Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.

My ecosystem.config.js looks like:

const os = require('os');
module.exports = {
    apps: [{
        port        : 3000,
        name        : "any",
        script      : "lib/src/index.js",
        watch       : true,           
        instances   : os.cpus().length,
        exec_mode   : 'fork',         
        env: {
            NODE_ENV: "production",
        }
    }]
}

index.js is a ES module using "import" syntax. How can i tell pm2 that is should use this way of importing

Branchia answered 22/12, 2021 at 14:8 Comment(0)
A
9

To achieve this you can create an intermediary CommonJS module which loads your application from ESModule. It's possible with import function available in commonJs modules.

This is how might this look:

  • ecosystem.config.js
  • lib/src/index.cjs CommonJS entry point (for PM2).
  • lib/src/index.js ESModule entry point (for ESM-compatible tools).
  • lib/src/app.js Application code.

ecosystem.config.js:

const os = require('os');
module.exports = {
    apps: [{
        port        : 3000,
        name        : "any",
        script      : "lib/src/index.cjs", // πŸ‘ˆ CommonJS
        watch       : true,           
        instances   : os.cpus().length,
        exec_mode   : 'fork',         
        env: {
            NODE_ENV: "production",
        }
    }]
}

lib/src/index.js:

import {app} from './app.js'

app()

lib/src/index.cjs:

import('./app.js') // πŸ‘ˆ There is import function available in CommonJS
.then(({app}) => {
 app()
})

lib/src/app.js:

import {hello} from './greet.js'

export function app() {
  console.log(hello('World'))
}

lib/src/greet.js:

export function hello(name) {
  return `Hello, ${name}!`
}
Acrodont answered 15/1, 2022 at 0:59 Comment(3)
Thx for the reply. Is there an easy way to use esm import within app.js when using CommonJs entry Point with PM2? For instance in app.js -> import {func} from "./other.js". I have not tried yet, but i guess it will be the same error again. Ill try tomorrow but maybe you know that. – Branchia
If export works then import would work too. I've updated example with such import. – Acrodont
Thank you very much. That works – Branchia
S
16

renaming ecosystem.config.js to ecosystem.config.cjs worked for me

Sherysherye answered 4/8, 2022 at 7:4 Comment(0)
A
9

To achieve this you can create an intermediary CommonJS module which loads your application from ESModule. It's possible with import function available in commonJs modules.

This is how might this look:

  • ecosystem.config.js
  • lib/src/index.cjs CommonJS entry point (for PM2).
  • lib/src/index.js ESModule entry point (for ESM-compatible tools).
  • lib/src/app.js Application code.

ecosystem.config.js:

const os = require('os');
module.exports = {
    apps: [{
        port        : 3000,
        name        : "any",
        script      : "lib/src/index.cjs", // πŸ‘ˆ CommonJS
        watch       : true,           
        instances   : os.cpus().length,
        exec_mode   : 'fork',         
        env: {
            NODE_ENV: "production",
        }
    }]
}

lib/src/index.js:

import {app} from './app.js'

app()

lib/src/index.cjs:

import('./app.js') // πŸ‘ˆ There is import function available in CommonJS
.then(({app}) => {
 app()
})

lib/src/app.js:

import {hello} from './greet.js'

export function app() {
  console.log(hello('World'))
}

lib/src/greet.js:

export function hello(name) {
  return `Hello, ${name}!`
}
Acrodont answered 15/1, 2022 at 0:59 Comment(3)
Thx for the reply. Is there an easy way to use esm import within app.js when using CommonJs entry Point with PM2? For instance in app.js -> import {func} from "./other.js". I have not tried yet, but i guess it will be the same error again. Ill try tomorrow but maybe you know that. – Branchia
If export works then import would work too. I've updated example with such import. – Acrodont
Thank you very much. That works – Branchia

© 2022 - 2024 β€” McMap. All rights reserved.