Pm2: Cannot read property pm2_env of undefined

Created on 13 May 2016  路  12Comments  路  Source: Unitech/pm2

Running pm2 (primarily from Ansible) is now giving this error on 1.1.3:

[PM2][WARN] Applications webpack not running, starting...
/usr/lib/node_modules/pm2/lib/CLI.js:461
        Common.printOut(cst.PREFIX_MSG + 'App [%s] launched (%d instances)', data[0].pm2_env.name, data.length);
                                                                                    ^

TypeError: Cannot read property 'pm2_env' of undefined
    at /usr/lib/node_modules/pm2/lib/CLI.js:461:85
    at /usr/lib/node_modules/pm2/node_modules/pm2-axon-rpc/lib/client.js:45:10
    at Parser.<anonymous> (/usr/lib/node_modules/pm2/node_modules/pm2-axon/lib/sockets/req.js:67:8)
    at emitOne (events.js:77:13)
    at Parser.emit (events.js:169:7)
    at Parser._write (/usr/lib/node_modules/pm2/node_modules/amp/lib/stream.js:91:16)
    at doWrite (_stream_writable.js:300:12)
    at writeOrBuffer (_stream_writable.js:286:5)
    at Parser.Writable.write (_stream_writable.js:214:11)
    at Socket.ondata (_stream_readable.js:536:20)

It turns out that data is an empty array.

Bug

Most helpful comment

Fix it by making sure pm2 has the privilege to write to the log folder.

All 12 comments

Here is the full debug output:

  pm2:constants PM2 folder (logs, pids, configuration): /home/vagrant/.pm2 +0ms
  pm2:interface:daemon Getting interaction info +152ms
  pm2:interface:daemon [PING INTERACTOR] Trying to connect to Interactor daemon +2ms
  pm2:satan [PING PM2] Trying to connect to server +6ms
  pm2:satan Daemon alive +6ms
  pm2:satan Launching RPC client on socket file /home/vagrant/.pm2/rpc.sock +1ms
  pm2:satan Connected to Daemon +2ms
  pm2:cli Got message from Satan as successfully connected to PM2, now parsing arguments +4ms
  pm2:satan Calling daemon method pm2:getVersion +1ms
  pm2:monit Before processing +10ms { cwd: '/var/may/core/',
  env: { WEBPACK_MODE: 'dev' },
  error_file: '/var/log/may/webpack.error.log',
  exec_interpreter: 'none',
  name: 'webpack',
  out_file: '/var/log/may/webpack.out.log',
  script: 'node_modules/.bin/webpack-dev-server',
  exec_mode: 'fork_mode' }
  pm2:monit After processing +17ms { errors: [],
  config:
   { script: 'node_modules/.bin/webpack-dev-server',
     name: 'webpack',
     error_file: '/var/log/may/webpack.error.log',
     out_file: '/var/log/may/webpack.out.log',
     cwd: '/var/may/core/',
     vizion: true,
     pmx: true,
     automation: true,
     autorestart: true,
     treekill: true,
     env: { WEBPACK_MODE: 'dev' },
     exec_mode: 'fork_mode',
     exec_interpreter: 'none' } }
  pm2:satan Calling daemon method pm2:getMonitorData +3ms
[PM2][WARN] Applications webpack not running, starting...
  pm2:satan Calling daemon method pm2:prepare +8ms
/usr/lib/node_modules/pm2/lib/CLI.js:461
        Common.printOut(cst.PREFIX_MSG + 'App [%s] launched (%d instances)', data[0].pm2_env.name, data.length);
                                                                                    ^

TypeError: Cannot read property 'pm2_env' of undefined
    at /usr/lib/node_modules/pm2/lib/CLI.js:461:85
    at /usr/lib/node_modules/pm2/node_modules/pm2-axon-rpc/lib/client.js:45:10
    at Parser.<anonymous> (/usr/lib/node_modules/pm2/node_modules/pm2-axon/lib/sockets/req.js:67:8)
    at emitOne (events.js:77:13)
    at Parser.emit (events.js:169:7)
    at Parser._write (/usr/lib/node_modules/pm2/node_modules/amp/lib/stream.js:91:16)
    at doWrite (_stream_writable.js:300:12)
    at writeOrBuffer (_stream_writable.js:286:5)
    at Parser.Writable.write (_stream_writable.js:214:11)
    at Socket.ondata (_stream_readable.js:536:20)

I'm getting the same error. Running 1.1.3 too.

Reverted to 1.0.0 and works again.

Same error.

Fix it by making sure pm2 has the privilege to write to the log folder.

Please make sure that you did pm2 update after installing the latest PM2 version

spot on @cnjsstong :+1: :)

Please make sure that you did pm2 update after installing the latest PM2 version

This was the fix to my problem. I included this as a post install step in the package.json to prevent this from happening again.

However, now I want to move my pm2 integration from shell scripts to a gulp tasks, and there is no command "update" in the PM2-API. What is the API equivalent to calling pm2 update from the console?

You should can do a pm2.update since its defined in the API here, the documentation isnt currently up-to-date, refer to the API object for accurate methods.

I'm failing to grasp how pm2 update is the solution here. I'm getting the exact same error, and this is a _fresh_ pm2 installation. There has never been any previous version of pm2 on the server. My understanding from the docs is that pm2 update forces the currently running pm2 god process to be replaced by an updated version, which makes sense in the case of an actual upgrade. In this case, it should be more akin to just stopping and starting the existing process, because the version has not changed. It does not at all seem to describe that it affects any configuration. Can you help me understand what I'm missing in what it's doing here?

@jchappell82 Cause it isnt the solution. We do write a lot to execute an pm2 update cause a lot of people do the update but dont reload the pm2 daemon.

The solution here has been wrote by @cnjsstong :
Fix it by making sure pm2 has the privilege to write to the log folder.

@vmarchaud It was the solution to my problem. The error occured only on a windows system, and folder permissions were not an issue there. Using pm2 update seemed to fix it. Maybe my problem is a different one? Here is the complete error message:

D:\someProject\node_modules\pm2\lib\API.js:979
        Common.printOut(conf.PREFIX_MSG + 'App [%s] launched (%d instances)', data[0].pm2_env.name, data.length);
                                                                                     ^

TypeError: Cannot read property 'pm2_env' of undefined
    at D:\someProject\node_modules\pm2\lib\API.js:979:86
    at D:\someProject\node_modules\pm2\node_modules\pm2-axon-rpc\lib\client.js:45:10
    at Parser.<anonymous> (D:\someProject\node_modules\pm2\node_modules\pm2-axon\lib\sockets\req.js:67:8)
    at emitOne (events.js:77:13)
    at Parser.emit (events.js:169:7)
    at Parser._write (D:\someProject\node_modules\pm2\node_modules\amp\lib\stream.js:91:16)
    at doWrite (_stream_writable.js:300:12)
    at writeOrBuffer (_stream_writable.js:286:5)
    at Parser.Writable.write (_stream_writable.js:214:11)
    at Socket.ondata (_stream_readable.js:536:20)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    at Pipe.onread (net.js:531:20)

The problem occurs only on Windows, and only once per reboot. Once I've done pm2 update there are no more problems.

update: pm2 kill also seems to do the trick.

@vmarchaud - Turns out you're right. Thanks! The actual issue it was having in my case was due to pm2 being run as a user who had just been added to the group that would have granted write permissions to the log folder, but had not created a new session due to the way my configuration management software (Ansible in this case) was processing commands.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

getvega picture getvega  路  3Comments

morfies picture morfies  路  3Comments

jubairsaidi picture jubairsaidi  路  3Comments

ghost picture ghost  路  3Comments

ldarren picture ldarren  路  3Comments