pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

package.json env params not loading into app

Open pablotor opened this issue 4 years ago • 1 comments

What's going wrong?

My app fails to start with pm2, due to package.json env params not loading

0|my-app | {"message":"Environment variable 'app.name' is not defined","level":"error"}
0|my-app | {"message":"Environment variable 'app.version' is not defined","level":"error"}
0|my-app | {"message":"Configuration check failed. Exiting","level":"error"}

Same params are correctly displayed by pm2 show 0:

 Describing process with id 0 - name my-app 
┌───────────────────┬─────────────────────────────────────────────────────────────────────┐
│ status            │ stopped                                                             │
│ name              │ my-app                                                              │
│ namespace         │ default                                                             │
│ version           │ 2.4.2                                                               │
│ restarts          │ 233                                                                 │
│ uptime            │ 0                                                                   │
│ script path       │ /opt/services/my-app/deploytest/source/my-app/dist/index.js         │
│ script args       │ N/A                                                                 │
│ error log path    │ /home/ubuntu/.pm2/logs/my-app-error.log                             │
│ out log path      │ /home/ubuntu/.pm2/logs/my-app-out.log                               │
│ pid path          │ /home/ubuntu/.pm2/pids/my-app-0.pid                                 │
│ interpreter       │ node                                                                │
│ interpreter args  │ N/A                                                                 │
│ script id         │ 0                                                                   │
│ exec cwd          │ /opt/services/my-app/deploytest/source/my-app                       │
│ exec mode         │ fork_mode                                                           │
│ node.js version   │ 10.17.0                                                             │
│ node env          │ N/A                                                                 │
│ watch & reload    │ ✘                                                                   │
│ unstable restarts │ 0                                                                   │
│ created at        │ 2021-01-14T16:07:21.841Z                                            │
└───────────────────┴─────────────────────────────────────────────────────────────────────┘
 Revision control metadata 
┌──────────────────┬────────────────────────────────────────────┐
│ revision control │ git                                        │
│ remote url       │ [email protected]:my-user/my-app.git          │
│ repository root  │ /opt/services/my-app/deploytest/source     │
│ last update      │ 2021-01-14T16:18:12.234Z                   │
│ revision         │ c8d1869c3db66c1689608f345de7c267610c691e   │
│ comment          │ Updated pm2 ecosystem file                 │
│ branch           │ staging                                    │
└──────────────────┴────────────────────────────────────────────┘
 Divergent env variables from local env 
┌─────┬────────────────────────────────────────────────────────┐
│ PWD │ /opt/services/my-app/deploytest/source/my-app/         │
└─────┴────────────────────────────────────────────────────────┘

This is my ecosystem.config.js file:

module.exports = {
  apps: [{
    name: 'my-app',
    cwd: '/opt/services/my-app/deploytest/source/my-app/',
    script: 'dist/index.js',
    watch: true,
    ignore_watch: ['node_modules'],
    post_update: ['npm install', 'npm run build'],
    env: {
      ENV_PATH: '/opt/services/my-app/.env',
    },
  }],
};

The workaround I found is to set ecosystem.config.js script to npm run serve (which just executes node dist/index.js) but then I lose that info on PM2:

 Describing process with id 0 - name my-app 
┌───────────────────┬───────────────────────────────────────────────────────┐
│ status            │ online                                                │
│ name              │ my-app                                                │
│ namespace         │ default                                               │
│ version           │ N/A                                                   │
│ restarts          │ 0                                                     │
│ uptime            │ 13m                                                   │
│ script path       │ /bin/bash                                             │
│ script args       │ -c npm run serve                                      │
│ error log path    │ /home/ubuntu/.pm2/logs/my-app-error.log               │
│ out log path      │ /home/ubuntu/.pm2/logs/my-app-out.log                 │
│ pid path          │ /home/ubuntu/.pm2/pids/my-app-0.pid                   │
│ interpreter       │ none                                                  │
│ interpreter args  │ N/A                                                   │
│ script id         │ 0                                                     │
│ exec cwd          │ /opt/services/my-app/deploytest/source/my-app         │
│ exec mode         │ fork_mode                                             │
│ node.js version   │ N/A                                                   │
│ node env          │ N/A                                                   │
│ watch & reload    │ ✔                                                     │
│ unstable restarts │ 0                                                     │
│ created at        │ 2021-01-14T15:35:45.159Z                              │
└───────────────────┴───────────────────────────────────────────────────────┘

How could we reproduce this issue?

I'm not sure what is causing the issue, but it might be related to my app's folder structure

root
└── my-app
    |
    ├── dist
    |   └── index.js
    |
    ├── ecosystem.config.js
    └── package.json

Just in case, this is my package.json

{
  "name": "my-app",
  "version": "2.4.2",
  "description": "'my-app API'",
  "main": "src/index.js",
  "engines": {
    "node": "=10"
  },
  "scripts": {
    "build": "babel src -d dist --copy-files",
    "serve": "node dist/index.js",
    "start": "nodemon src/index.js",
  },

Supporting information

$ pm2 report

--- PM2 report ----------------------------------------------------------------
Date                 : Thu Jan 14 2021 20:03:53 GMT+0000 (Coordinated Universal Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 4.5.1
node version         : 10.17.0
node path            : /usr/local/bin/pm2
argv                 : /usr/local/bin/node,/usr/local/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
uptime               : 1378min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 4.5.1
node version         : 10.17.0
node path            : /usr/local/bin/pm2
argv                 : /usr/local/bin/node,/usr/local/bin/pm2,report
argv0                : node
user                 : ubuntu
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) CPU E5-2686 v4 @ 2.30GHz
cpus nb              : 2
freemem              : 764043264
totalmem             : 4135108608
home                 : /home/ubuntu
===============================================================================
--- PM2 list -----------------------------------------------
┌─────┬───────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name          │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ my-app        │ default     │ 2.4.2   │ fork    │ 0        │ 0      │ 233  │ stopped   │ 0%       │ 0b       │ ubuntu   │ disabled │
└─────┴───────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/home/ubuntu/.pm2/pm2.log last 20 lines:
PM2        | 2021-01-14T16:17:58: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:01: PM2 log: App [my-app:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-01-14T16:18:01: PM2 log: App [my-app:0] starting in -fork mode-
PM2        | 2021-01-14T16:18:01: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:03: PM2 log: App [my-app:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-01-14T16:18:03: PM2 log: App [my-app:0] starting in -fork mode-
PM2        | 2021-01-14T16:18:03: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:06: PM2 log: App [my-app:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-01-14T16:18:06: PM2 log: App [my-app:0] starting in -fork mode-
PM2        | 2021-01-14T16:18:06: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:09: PM2 log: App [my-app:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-01-14T16:18:09: PM2 log: App [my-app:0] starting in -fork mode-
PM2        | 2021-01-14T16:18:09: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:12: PM2 log: App [my-app:0] exited with code [1] via signal [SIGINT]
PM2        | 2021-01-14T16:18:12: PM2 log: App [my-app:0] starting in -fork mode-
PM2        | 2021-01-14T16:18:12: PM2 log: App [my-app:0] online
PM2        | 2021-01-14T16:18:13: PM2 log: [Watch] Stop watching my-app
PM2        | 2021-01-14T16:18:13: PM2 log: Stopping app:my-app id:0
PM2        | 2021-01-14T16:18:14: PM2 log: App [my-app:0] exited with code [0] via signal [SIGINT]
PM2        | 2021-01-14T16:18:14: PM2 log: pid=11084 msg=process killed

pablotor avatar Jan 14 '21 20:01 pablotor

hi, same issue : how to recover the name and version of the package to use is as display it in response to a front-end ? the "process.env.npm_package_version" displays "undefined" with pm2

Productivix avatar Jun 08 '22 10:06 Productivix