问题
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)
回答1:
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.
回答2:
After upgrading node to v12 (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>"
回答3:
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
来源:https://stackoverflow.com/questions/59834021/pm2-unexpected-token-import