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

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

Open
dhowe opened this issue Jan 20, 2020 · 9 comments
Open

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

dhowe opened this issue Jan 20, 2020 · 9 comments

Comments

@dhowe
Copy link

dhowe commented Jan 20, 2020

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
Copy link

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

@dhowe
Copy link
Author

dhowe commented May 31, 2020

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
Copy link

aksel commented Jun 26, 2020

@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
Copy link

brecke commented Oct 27, 2020

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
Copy link

rellampec commented Mar 10, 2021

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
  • removing the .pm2 folder was necessary (reinstall alone would not solve it)

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

@krunkosaurus
Copy link

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

@AugustodeAraujo
Copy link

For some reason @rellampec response worker here too!

@HagTheDon
Copy link

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.

@rellampec
Copy link

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

@AugustodeAraujo good point. Issue reported here

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

No branches or pull requests

8 participants