12

I have a webserver that works when I use node or nodemon (e.g. "nodemon index.js"). However, when I try to use pm2 ("pm2 start index.js"), I get "SyntaxError: Unexpected token import". The full error log is below. What am I doing wrong here?

/usr/local/lib/node_modules/pm2/lib/ProcessContainerFork.js:29
import(process.env.pm_exec_path);
^^^^^^

SyntaxError: Unexpected token import 
at new Script (vm.js:51:7)
at createScript (vm.js:136:10)
at Object.runInThisContext (vm.js:197:10)
at Module._compile (internal/modules/cjs/loader.js:618:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:665:10)
at Module.load (internal/modules/cjs/loader.js:566:32)
at tryModuleLoad (internal/modules/cjs/loader.js:506:12)
at Function.Module._load (internal/modules/cjs/loader.js:498:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:695:10)
at startup (internal/bootstrap/node.js:201:19)
Jeff
  • 127
  • 1
  • 1
  • 5

7 Answers7

20

Hit the same issue.

pm2 released version 4.2.2 which only works with Node 10.x or better, so:

Best solution is to upgrade your node from 9.x to 10.x or better.

In my case I wanted to stick to node 9 so I fixed the version of pm2 to version 4.2.1

I use npm to install pm2 in my Dockerfile:

Changing:

RUN npm install -g webpack@4.29.3 pm2

To:

RUN npm install -g webpack@4.29.3 pm2@4.2.1

Will fix the issue and allow you to continue working with node 9 and pm2 4.2.1

If you install pm2 in some other way post your install details and I can recommend how to fix.

AAber
  • 1,562
  • 10
  • 14
  • 1
    Reported issue on github - https://github.com/Unitech/pm2/issues/4588#ref-commit-278d4b2 - I see on node 9.x will works too after next release on pm2. – krystianj Jan 21 '20 at 13:47
13

After upgrading node (and reinstalling pm2) it seems like pm2 was trying to run my services using the old configuration it had saved. All I had to do was delete and start them fresh.

pm2 delete <app-name>
pm2 start src/app.js --name="<app-name>"

Alternatively, if you reinstall and get an error that says Current process list is not synchronized with saved list you can also use:

pm2 resurrect
spencer.sm
  • 19,173
  • 10
  • 77
  • 88
7

THIS ANSWER IS FOR DEALING WITH LEGACY SYSTEMS

As others pointed out, the error is a product of incompatibility between node version and pm2 version. For example, this issue can be caused when working on a legacy project, where pm2 gets updated but the node.js version is old.

It's not always possible to "just upgrade the node version", even though it would be the best thing to do. Therefore in case you run pm2 and it will give you an error such as:

$ pm2 --version

/usr/local/nvm/v6.3.0/lib/node_modules/pm2/node_modules/chalk/source/index.js:103
  ...styles,
  ^^^
SyntaxError: Unexpected token ...
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/usr/local/nvm/v6.3.0/lib/node_modules/pm2/constants.js:10:14)
    at Module._compile (module.js:541:32)

You can fix first the issue now, and then plan an upgrade later.

  1. You can get the currently installed version of your npm packages which will contain also your pm2 package with its version with the command:
$ npm list -g --depth 0

/usr/local/lib
├── other-package@version.number
└── pm2@4.2.2
├── ...
└── ...

  1. Then after checking that it is a problem related to the versioning, simply downgrade pm2 to get it back to work by installing globally a specific version of pm2 (that is compatible with your node):
$ npm install -g pm2@3.5.2 # might need sudo

You can verify with the installation is successful by running $ pm2 --version or the command in step one $ npm list -g --depth 0

Tip: The best way to figure out which version you need is probably to check pm2 changelogs and see which pm2 release has changed node version.

Fed
  • 1,696
  • 22
  • 29
1

For the latest pm2,u need to create an ecosystem.config.js file,the content is like

module.exports = {
  apps : [{
    name: "mp-todo",
    script: "./build/index.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    },
    log_date_format: 'YYYY-MM-DD HH:mm Z',
    combine_logs: true
  }]
}

and u can use pm2 start ecosystem.config.js --env production to use the environment variables in the config file

crazyones110
  • 296
  • 3
  • 18
1

I had below error:

/root/.nvm/versions/node/v8.0.0/lib/node_modules/pm2/node_modules/chalk/source/index.js:103
    ...styles,
    ^^^ SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/root/.nvm/versions/node/v8.0.0/lib/node_modules/pm2/constants.js:10:14)

pm2 supported version 4.1.2 has some issue with Node version 8, We must upgrade node version to at least 12 to work it properly. I did the same and it worked for me.

Note: Node. js versions are mostly backward compatible, meaning that code you wrote for Node 8 will work on Node 10 or 12. Thus, if you only have plain old JavaScript you should face no difficulties upgrading

0

The workaround I found is to run it with Windows PowerShell. I was running it with Git Bash and it was not working, giving the same errors above. After running with PowerShell, it started correctly, with all the instances I wanted.

Vitor Braga
  • 2,173
  • 1
  • 23
  • 19
  • That probably means you managed to install 2 versions of Node on your machine, and PowerShell sees a different version than Git Bash does. – aggregate1166877 Nov 15 '22 at 05:05
-1

i have problem like u, but i fixed. I update nodejs to latest version. Please enter these 3 commands :

  • sudo npm cache clean -f
  • sudo npm install -g n
  • sudo n stable

so reset it. Your problem will be solved