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.
- 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]
├── ...
└── ...
- 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.