chovy
chovy

Reputation: 75686

pm2 ecosystem file [PM2][ERROR] File ./ecosystem.config.js malformated

import cfg from 'dotenv-flow';

cfg.config();

const {
    HOST_USER,
    HOST_DOMAIN,
    HOST_PATH,
    HOST_PROJECT,
    PORT,
    VITE_API_ENDPOINT,
    APP_URL,
    NODE_ENV
} = process.env;

const env_production = {
    HOST_USER,
    HOST_DOMAIN,
    HOST_PATH,
    HOST_PROJECT,
    PORT,
    VITE_API_ENDPOINT,
    APP_URL,
    NODE_ENV
};

console.log(env_production);
module.exports = {
    apps: [
        {
            name: 'grazily-ui',
            script: './build/index.js',
            env_production
        }
    ]
};

Getting this error:

┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ index              │ fork     │ 0    │ stopped   │ 0%       │ 0b       │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
[PM2][ERROR] File ecosystem.config.js malformated
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/ettinger/www/grazily.com/grazily-ui/ecosystem.config.js from /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/Common.js not supported.
Instead change the require of ecosystem.config.js in /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/Common.js to a dynamic import() which is available in all CommonJS modules.
    at Object.Common.parseConfig (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/Common.js:317:12)
    at API._startJson (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/API.js:934:25)
    at API.start (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/API.js:329:12)
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/binaries/CLI.js:290:13
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/async/internal/withoutIndex.js:8:40
    at replenish (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:81:17)
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/async/internal/eachOfLimit.js:86:9
    at eachLimit (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/async/forEachLimit.js:47:43)
    at awaitable (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/async/internal/awaitify.js:13:28)
    at Command.<anonymous> (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/binaries/CLI.js:289:7)
    at Command.listener (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/commander/index.js:315:8)
    at Command.emit (node:events:526:28)
    at Command.parseArgs (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/commander/index.js:651:12)
    at Command.parse (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/node_modules/commander/index.js:474:21)
    at beginCommandProcessing (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/binaries/CLI.js:147:13)
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/binaries/CLI.js:221:7
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/API.js:181:16
    at /home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/Client.js:54:16
    at Timeout._onTimeout (/home/ettinger/.nvm/versions/node/v17.5.0/lib/node_modules/pm2/lib/Client.js:372:9) {
  code: 'ERR_REQUIRE_ESM'
}

edit: I opted to just use systemd with a bash script running node index.js

Upvotes: 3

Views: 8823

Answers (2)

np42
np42

Reputation: 947

You should rename the file ecosystem.config.js to ecosystem.config.cjs when using es module ("type": "module" in pacakge.json)

Upvotes: 16

Paul Thomas
Paul Thomas

Reputation: 81

Use const cfg = require('dotenv-flow')

Upvotes: 0

Related Questions