skip to Main Content

I am building a Docker image for the Node.js app. The issue is when I try to run it, I am getting an error as:

on:0,verify:0,log:0],prefetch=(available=true,default=false),"}}
backend-server  | 2024-10-13T19:23:58: PM2 log: Launching in no daemon mode
backend-server  | 2024-10-13T19:23:58: PM2 error: SyntaxError: Unexpected token 'export'
backend-server  | 2024-10-13T19:23:58: PM2 error: Unexpected token 'export'
egory_id":34,"verbose_level":"DEBUG_1","verbose_level_id":1,"msg":"Recovering log 5 through 5"}}}
backend-server  | 2024-10-13T19:23:58: PM2 log: PM2 successfully stopped

i tried replacing file name with cjs and it dosent works as expected
the file is
// pm2.config.js
export default { apps: [ { name: "express-app-ecom", script: "npm start", instances: "max", exec_mode: "cluster", env: { NODE_ENV: "development" }, env_production: { NODE_ENV: "production" } } ] };

Any help will be appreciated

2

Answers


  1. You can replace export default to the module.default to use CommonJS module exports

    module.exports = {
      apps: [
        {
          name: "express-app-ecom",
          script: "npm start",
          instances: "max",
          exec_mode: "cluster",
          env: {
            NODE_ENV: "development",
          },
          env_production: {
            NODE_ENV: "production",
          },
        },
      ],
    };
    
    Login or Signup to reply.
  2. Okay, The error you’re encountering is due to the use of ES6 export default syntax in the pm2.config.js file, which isn’t natively supported in Node.js unless you enable ECMAScript modules (ESM) by using .mjs files or enabling ESM through the package.json file.

    The solution would be to convert the file to commonjs.
    Syntax:
    module.exports=[]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search