standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 146 forks source link

Error with pm2: Cannot find module 'esm' #861

Open dhowe opened 4 years ago

dhowe commented 4 years ago

Launched via $ pm2 start ecosystem.config.js, with config:

module.exports = {
  apps : [{
    name: 'specserv',
    script: 'server.js',
    instances: 1,
    node_args: '-r esm',
    autorestart: true,
    watch: true,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }]
};
--- PM2 report ----------------------------------------------------------------
Date                 : Mon Jan 20 2020 12:48:32 GMT+0800 
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 4.2.1
node version         : 12.9.1
node path            : /Users/mono1/.config/yarn/global/node_modules/pm2/bin/pm2
argv                 : /Users/mono1/.nvm/versions/node/v12.9.1/bin/node,/Users/mono1/.config/yarn/global/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : mono1
uid                  : 501
gid                  : 20
uptime               : 19min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 4.2.1
node version         : 12.9.1
node path            : /usr/local/Homebrew/bin/pm2
argv                 : /Users/mono1/.nvm/versions/node/v12.9.1/bin/node,/usr/local/Homebrew/bin/pm2,report
argv0                : node
user                 : dhowe
uid                  : 501
gid                  : 20
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : darwin
type                 : Darwin
cpus                 : Intel(R) Core(TM) i3-8100B CPU @ 3.60GHz
cpus nb              : 4
freemem              : 2399825920
totalmem             : 8589934592
home                 : /Users/mono1
===============================================================================
--- PM2 list -----------------------------------------------
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ spectre-server     │ cluster  │ 4788 │ errored   │ 0%       │ 0b       │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/Users/mono1/.pm2/pm2.log last 20 lines:
PM2        | internal/modules/cjs/loader.js:775
PM2        |     throw err;
PM2        |     ^
PM2        | 
PM2        | Error: Cannot find module 'esm'
PM2        | Require stack:
PM2        | - internal/preload
PM2        |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:772:15)
PM2        |     at Function.Module._load (internal/modules/cjs/loader.js:677:27)
PM2        |     at Module.require (internal/modules/cjs/loader.js:830:19)
PM2        |     at Module._preloadModules (internal/modules/cjs/loader.js:1099:12)
PM2        |     at loadPreloadModules (internal/bootstrap/pre_execution.js:419:5)
PM2        |     at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:54:3)
PM2        |     at internal/main/run_main_module.js:7:1 {
PM2        |   code: 'MODULE_NOT_FOUND',
PM2        |   requireStack: [ 'internal/preload' ]
PM2        | }
PM2        | 2020-01-20T12:32:33: PM2 log: App name:spectre-server id:0 disconnected
PM2        | 2020-01-20T12:32:33: PM2 log: App [spectre-server:0] exited with code [1] via signal [SIGINT]
PM2        | 2020-01-20T12:32:33: PM2 log: Script /Users/mono1/git/spectre/server.js had too many unstable restarts (16). Stopped. "errored"

Please copy/paste the above report in your issue on https://github.com/Unitech/pm2/issues

david-saint commented 4 years ago

HI @dhowe, did you manage to solve this issue?

dhowe commented 4 years ago

I think (it was > 5 month ago that I posted this ticket) that removing the 'instances' field did it, though better documentation would have been useful. Current config:


module.exports = {
  apps : [{
    name: 'specserv',
    log_file: 'specserv.log',
    script: 'server.js',
    node_args: '--require=esm',
    autorestart: true,
    watch: false,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    }
  }]
};
aksel commented 4 years ago

@david-saint I had an issue, where everything worked in fork mode, but cluster mode did not.

Turns out all I had to do was make a node_modules folder in ~/.pm2

mkdir -p ~/.pm2/node_modules

No clue why this happens. A lot of frustrating hours wasted.

brecke commented 3 years ago

Same issue here, solved by changing from cluster mode to fork as suggested by @dhowe. No idea why this happens, seems to be a new bug since I'm pretty sure this has worked before.

rellampec commented 3 years ago

For some reason the pm2 metadata stored in ~/.pm2 got something wrong at some stage.

The way I could solve this particular issue, with no working-around needed (preserving everything in my project as it was):

npm uninstall -g pm2
rm -r ~/.pm2
npm install -g pm2

After the above, pm2 start ecosystem.config.js worked again with esm.

krunkosaurus commented 2 years ago

Super weird issue but @rellampec 's response worked for me. Has anybody notified PM2?

AugustodeAraujo commented 2 years ago

For some reason @rellampec response worker here too!

HagTheDon commented 1 year ago

For anyone still looking at this in 2022, I solved it by installing the esm package at the project root with; cd ~ npm i esm

Try this if all else fails. I think pm2 searches for the esm package at the project root not in the project folder.