Unitech / Unitech/pm2

package.json env params not loading into app

Open
#4,960 1 comment 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with ecosystem.config.js and package.json, then compare PM2 4.5.1 running dist/index.js directly with the npm run serve workaround. Reproduce using the supplied Node.js 10 setup and pm2 report output; done means the configured environment reaches the application without losing PM2 process metadata, with a regression test if the repository provides a relevant test entry point.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
devops
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.