Unable to start pm2 via npm script
What's going wrong?
Using pm2 start works when run via the command line directly (whether its a global or local installation of pm2) but not when run via an npm script. This affects JavaScript and JSON based configuration as well as a node script that calls the pm2 api.
How could we reproduce this issue?
- Create a node project with pm2 as a dependency
- Create a simple ecosystem configuration file
- Set the start script for the project to
pm2 start ecosystem.config.js
Supporting information
The configuration being used:
module.exports = {
apps: [{
script: 'src/main.js',
name: 'CWS',
instances: 2,
exec_mode: 'cluster',
autorestart: false
}]
}
The follow error appears in the pm2.log:
undefined:1
undefined
^
SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (D:\Projects\async\claims-worker-service\dist\node_modules\pm2\lib\ProcessContainer.js:17:20)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Function.Module.runMain (module.js:676:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
It appears that the pm2_env variable is undefined when the app is run via npm script. My suspicion was that npm was somehow poisoning the environment variables somehow. I inserted delete env_copy.env on line 47 of pm2\lib\God\ClusterMode.js and that resolve the issue so I wonder if somehow the pm2_env string is too long to be passed via fork?
Update I added something to the node script that I am calling the PM2 API from that removes all the npm environment variables my app doesn't use. This also fixes the issue. I'm still not sure if its length or some sort of conflict, but I'm leaning towards length.
Please run the following command (available on PM2 >= 2.6)
$ pm2 report
Report run via pm2 directly:
[PM2] Spawning PM2 daemon with pm2_home=H:\.pm2
[PM2] PM2 Successfully daemonized
===============================================================================
--- PM2 REPORT (Mon Mar 05 2018 17:47:36 GMT+1000 (E. Australia Standard Time))
===============================================================================
--- Daemon --------------------------------------------------------------------
pm2d version : 2.10.1
node version : 8.9.1
node path : D:/Apps/nodejs-8/node
argv : D:\Apps\nodejs-8\node.exe,D:\Projects\async\claims-worker-service\dist\node_modules\pm2\lib\Daemon.js
argv0 : node
user : undefined
uid : N/A
gid : N/A
uptime : 0min
===============================================================================
--- CLI -----------------------------------------------------------------------
local pm2 : 2.10.1
node version : 8.9.1
node path : D:/Apps/nodejs-8/node
argv : D:\Apps\nodejs-8\node.exe,D:\Projects\async\claims-worker-service\dist\node_modules\pm2\bin\pm2,report
argv0 : D:\Apps\nodejs-8\node.exe
user : u386220
===============================================================================
--- System info ---------------------------------------------------------------
arch : x64
platform : win32
type : Windows_NT
cpus : Intel(R) Xeon(R) CPU E5-2660 v2 @ 2.20GHz
cpus nb : 4
freemem : 3624583168
totalmem : 17179402240
home : C:\Users\u386220
===============================================================================
--- PM2 list ------------------------------------------------------------------
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
===============================================================================
--- Daemon logs ---------------------------------------------------------------
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
Report run via npm script:
running better-npm-run in D:\Projects\async\claims-worker-service\dist
Executing script: pm2
to be executed: "pm2 report"
===============================================================================
--- PM2 REPORT (Mon Mar 05 2018 17:48:29 GMT+1000 (E. Australia Standard Time))
===============================================================================
--- Daemon --------------------------------------------------------------------
pm2d version : 2.10.1
node version : 8.9.1
node path : D:/Apps/nodejs-8/node
argv : D:\Apps\nodejs-8\node.exe,D:\Projects\async\claims-worker-service\dist\node_modules\pm2\lib\Daemon.js
argv0 : node
user : undefined
uid : N/A
gid : N/A
uptime : 0min
===============================================================================
--- CLI -----------------------------------------------------------------------
local pm2 : 2.10.1
node version : 8.9.1
node path : D:/Apps/nodejs-8/node.exe
argv : D:\Apps\nodejs-8\node.exe,D:\Projects\async\claims-worker-service\dist\node_modules\pm2\bin\pm2,report
argv0 : node
user : u386220
===============================================================================
--- System info ---------------------------------------------------------------
arch : x64
platform : win32
type : Windows_NT
cpus : Intel(R) Xeon(R) CPU E5-2660 v2 @ 2.20GHz
cpus nb : 4
freemem : 3544248320
totalmem : 17179402240
home : C:\Users\u386220
===============================================================================
--- PM2 list ------------------------------------------------------------------
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
===============================================================================
--- Daemon logs ---------------------------------------------------------------
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
I get the same error due to a null process.env.pm2_env when starting programmatically in cluster mode:
pm2.start({
script: __dirname+'/server.js',
name: 'flexit',
exec_mode: 'cluster',
instances: 4,
min_uptime: '60s',
max_restarts: 15,
out_file: filePath,
error_file: filePath,
merge_logs: true,
log_date_format: 'YYYY-MM-DD HH:mm Z'
}, function (err, apps) {
if (err) {
console.log('start err: ', err);
} else {
console.log('start successful');
}
pm2.disconnect();
});
Here's my PM2 report:
===============================================================================
--- PM2 REPORT (Thu Jul 26 2018 07:12:06 GMT-0700 (Pacific Daylight Time)) ----
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 3.0.1
node version : 10.7.0
node path : not found
argv : C:\Program Files\nodejs\node.exe,C:\apps\flexbiserver\node_modules\pm2\lib\Daemon.js
argv0 : node
user : undefined
uid : N/A
gid : N/A
uptime : 23min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 3.0.1
node version : 10.7.0
node path : not found
argv : C:\Program Files\nodejs\node.exe,C:\Users\andre\AppData\Roaming\npm\node_modules\pm2\bin\pm2,report
argv0 : node
user : ataft
===============================================================================
--- System info --------------------------------------------
arch : x64
platform : win32
type : Windows_NT
cpus : Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
cpus nb : 4
freemem : 10851577856
totalmem : 17027444736
home : C:\Users\andre
===============================================================================
--- PM2 list -----------------------------------------------
┌──────────┬────┬─────────┬─────┬─────────┬─────────┬────────┬─────┬────────┬───────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼─────┼─────────┼─────────┼────────┼─────┼────────┼───────┼──────────┤
│ flexit │ 0 │ cluster │ 0 │ errored │ 70 │ 0 │ 0% │ 0 B │ ataft │ disabled │
└──────────┴────┴─────────┴─────┴─────────┴─────────┴────────┴─────┴────────┴───────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
┌──────────┬────┬─────────┬─────┬─────────┬─────────┬────────┬─────┬────────┬───────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
├──────────┼────┼─────────┼─────┼─────────┼─────────┼────────┼─────┼────────┼───────┼──────────┤
│ flexit │ 0 │ cluster │ 0 │ errored │ 70 │ 0 │ 0% │ 0 B │ ataft │ disabled │
└──────────┴────┴─────────┴─────┴─────────┴─────────┴────────┴─────┴────────┴───────┴──────────┘
Update:
I got it to work fine with node v10.7.0 and pm2 v3.0.1 by using the "node" command instead of "npm".
This works: node . start
This throws the error mentioned above: npm run start (the start script in package.json is "node . start"). My npm version is v6.1.0
@NSGReaper It's interesting that you were getting this error with pm2 v2.10.1 and node v8.9.1. I had those exact same versions when my app was working and only started having this issue after upgrading to newer versions.
Hi @NSGReaper @ataft, having same issue here.
pm2 start server/index.js -i max => works (although opens several node empty dialog boxes...)
But, npm run pm2 assigned to exactly above => Doesn't work!
So I enter pm2 monit, the logs show pm2 clusters crashing and restarting due to the infamous node Error: listen EADDRINUSE port error.
Windows 10
"node": "8.9.4"
"pm2": "^3.0.3",
Any solution?
-EDIT-
Both npm and direct CLI commands are working. Have no idea why...
Hi, I couldn't explain why, but I got this error when adding changes to Readme file. After reverting changes for this file this error is disappeared.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
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.