pm2 Unexpected token import
Asked Answered
P

7

13

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)
Pericranium answered 21/1, 2020 at 4:3 Comment(2)
What is your nodejs version?Phytography
I have the same problem, Yesterday working today doesn't work. My nodejs version is 9.4.0. P.S. I see yesterday pm2 was updated.Done
P
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 [email protected] pm2

To:

RUN npm install -g [email protected] [email protected]

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.

Planimeter answered 21/1, 2020 at 11:54 Comment(1)
Reported issue on github - github.com/Unitech/pm2/issues/4588#ref-commit-278d4b2 - I see on node 9.x will works too after next release on pm2.Done
S
15

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
Shouldst answered 7/4, 2020 at 18:6 Comment(3)
Thanks man, you saved me! i have upgraded NodeJS from 8.x to 10.x and reinstall pm2 then did "pm2 resurrect" to get my pm2 processes back and this error occurredWellchosen
And if you have running processes that don't show up with pm2 ls then you can run pm2 delete all to start fresh.Shouldst
thank you, should add this in accepted answerKali
B
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
├── [email protected]
└── [email protected]
├── ...
└── ...

  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 [email protected] # 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.

Baeyer answered 5/7, 2021 at 23:10 Comment(0)
D
1

For the latest pm2,you 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 you can use pm2 start ecosystem.config.js --env production to use the environment variables in the config file

Dragoon answered 9/3, 2020 at 9:52 Comment(0)
E
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

Endometriosis answered 19/9, 2021 at 18:21 Comment(0)
C
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.

Condensate answered 28/9, 2020 at 22:7 Comment(1)
That probably means you managed to install 2 versions of Node on your machine, and PowerShell sees a different version than Git Bash does.Resting
L
-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

Lucifer answered 10/12, 2021 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.