Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package.json env params not loading into app #4960

Open
pablotor opened this issue Jan 14, 2021 · 1 comment
Open

package.json env params not loading into app #4960

pablotor opened this issue Jan 14, 2021 · 1 comment

Comments

@pablotor
Copy link

pablotor commented Jan 14, 2021

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       │ git@github.com: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

@Productivix
Copy link

Productivix commented Jun 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants