Unitech / Unitech/pm2

Cluster mode does not change working directory

Open
#5,722 3 comments 2 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?

I need to start a node.js server and i have to specify a --import argument to node.

This works well in fork mode, but it fails in cluster mode. As far as i know the "cwd" is not applied early enough and make --import does not search in the good folders.

How could we reproduce this issue?

  • Create an ecosystem.js file containing :
module.exports = {
  apps: [{                   
    name: 'appTest',
    script: 'index.js',
    cwd: '/path/to/appTest/',
    node_args: '--import=specifier-resolution-node/register',

    instances: 'max', // Works when this line is disabled
  }],
};
  • Be sure appTest has a module compatible with --import like specifier-resolution-node

  • Try to start ecosystem.js from somewhere else than /path/to/appTest/

  • You can see that the process enters in "errored" mode, and if you watch in .pm2/pm2.log you will see this error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'specifier-resolution-node' imported from /tmp/scripts/pm2/
    at packageResolve (node:internal/modules/esm/resolve:844:9)
    at moduleResolve (node:internal/modules/esm/resolve:901:20)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:328:34)
    at node:internal/process/esm_loader:26:84
    at node:internal/per_context/primordials:544:39
    at new Promise (<anonymous>) {
  code: 'ERR_MODULE_NOT_FOUND'
}
  • I'm nearly sure node.js is trying to import the package but can't locate the node_modules since it has the wrong working directory

Supporting information

--- PM2 report ----------------------------------------------------------------            
Date                 : Fri Dec 01 2023 21:37:20 GMT+0000 (Coordinated Universal Time)                                                                                                                               
===============================================================================
--- Daemon -------------------------------------------------             
pm2d version         : 5.3.0                                                                              
node version         : 20.10.0                                                                            
node path            : /usr/bin/pm2                                                                       
argv                 : /usr/bin/node,/usr/lib/node_modules/pm2/lib/Daemon.js
argv0                : node                                                                               
user                 : root                                                                               
uid                  : 0
gid                  : 0
uptime               : 237min
===============================================================================
--- CLI ----------------------------------------------------                             
local pm2            : 5.3.0
node version         : 20.10.0
node path            : /usr/bin/pm2                                                                                                                                                                                 
argv                 : /usr/bin/node,/usr/bin/pm2,report                     
argv0                : node
user                 : root
uid                  : 0
gid                  : 0
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : Intel(R) Xeon(R) CPU E3-1270 v6 @ 3.80GHz
cpus nb              : 8
freemem              : 31666073600
totalmem             : 33402691584
home                 : /root

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

Reproduce the issue with the provided ecosystem.js configuration, comparing fork mode with cluster mode and checking .pm2/pm2.log. Start by tracing the cluster startup path around cwd handling and Node's --import initialization. Done means the imported package resolves from /path/to/appTest/ when the process is started from another directory.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.