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)
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.
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
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.
$ npm list -g --depth 0
/usr/local/lib
├── [email protected]
└── [email protected]
├── ...
└── ...
$ 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With