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

pm2 start app.js -i 0 fails because Error: ENOENT, no such file or directory for process.cwd() #1244

Closed
scottlingran opened this issue May 7, 2015 · 25 comments

Comments

@scottlingran
Copy link

I am running the PM2 way example here - https://keymetrics.io/2015/03/26/pm2-clustering-made-easy/

var http = require('http');

http.createServer(function(req, res) {  
  res.writeHead(200);
  res.end("hello world");
}).listen(8080);
$ pm2 start app.js -i 0

However, the process fails 15 times and is then stopped. Here are the logs:

PM2: 2015-05-07 15:38:53: Starting execution sequence in -cluster mode- for app name:app id:22
PM2: node.js:720
PM2:     var cwd = process.cwd();
PM2:                       ^
PM2: Error: ENOENT, no such file or directory
PM2:     at Error (native)
PM2:     at Function.startup.resolveArgv0 (node.js:720:23)
PM2:     at startup (node.js:63:13)
PM2:     at node.js:814:3
PM2: 2015-05-07 15:38:53: App name:cluster.pm2 id:22 disconnected
PM2: 2015-05-07 15:38:53: App name:cluster.pm2 id:22 exited with code 1

Node 0.12.2
PM2 0.12.14
OSX 10.10.3

@jshkurti
Copy link
Contributor

jshkurti commented May 7, 2015

Open a new terminal and try again.
If still not working, does the command node alone work ?

@jshkurti
Copy link
Contributor

jshkurti commented May 7, 2015

@scottlingran
Copy link
Author

Yeah, node and pm2 start app.js work just fine. Confused why cluster mode doesn't work.

@jshkurti
Copy link
Contributor

Your might need to re-install pm2 again i guess.

@scottlingran
Copy link
Author

Yeah I think it's just me. Tried it in another env and it was fine. Thanks for the help though :)

@Jokero
Copy link

Jokero commented Sep 9, 2015

I rarely have same issue. node app.js and pm2 start app.js works. But pm2 start -i 0 does not work:

PM2: 2015-09-09 17:42:48: Starting execution sequence in -cluster mode- for app name:app id:0
PM2: node.js:720
PM2:     var cwd = process.cwd();
PM2:                       ^
PM2: Error: ENOENT, no such file or directory
PM2:     at Error (native)
PM2:     at Function.startup.resolveArgv0 (node.js:720:23)
PM2:     at startup (node.js:63:13)
PM2:     at node.js:814:3
PM2: 2015-09-09 17:42:48: App name:app id:0 disconnected
PM2: 2015-09-09 17:42:48: App name:app id:0 exited with code 1

I can't reproduce it. pm2 reinstalling is very bad because it happened on production

@soyuka
Copy link
Collaborator

soyuka commented Sep 9, 2015

This is not related to pm2, try from another directory or start from a json declaration : http://pm2.keymetrics.io/docs/usage/application-declaration/ by specifying a cwd.

@Jokero
Copy link

Jokero commented Sep 9, 2015

I really use pm2 startOrGracefulReload pm2.json

pm2.json:

{
  "apps": [
    {
      "name":            "api",
      "cwd":             "/apps/api/current",
      "script":          "app.js",
      "exec_mode":       "cluster",
      "instances":       0,

      "out_file":        "/var/log/apps/api/output.log",
      "error_file":      "/var/log/apps/api/error.log",
      "log_date_format": "YYYY-MM-DD HH:mm:ss",
      "merge_logs":      true
    },
    ...
]

So result is the same

@soyuka
Copy link
Collaborator

soyuka commented Sep 9, 2015

All I can say is that nodejs does not find /apps/api/current

@Jokero
Copy link

Jokero commented Sep 9, 2015

But directory exists :)

@Jokero
Copy link

Jokero commented Sep 9, 2015

And it's very strange that after pm2 reinstalling all is working

@yinrong
Copy link

yinrong commented Sep 17, 2015

same issue, a big headache.

I can't delete pm2 since I have to keep some of the processes running

@vjpr
Copy link

vjpr commented Jan 31, 2016

I ran into the same issue. The problem is when you run pm2 from a symlinked dir. Not exactly sure what is going on yet. I'm thinking that running pm2 start outside of the symlinked dir would fix it.

@ColinNiu
Copy link

ColinNiu commented Apr 8, 2016

I ran into this issue too. ** It finally worked after run ** pm2 kill
In my case, I first ran it in "fork" mode, works fine, and then changed to "cluster" mode, encounted this problem.
tried reinstalling pm2 with no luck;
tried run from the real dir (not the symlinked dir), with noluck.

@lvdaqian
Copy link

lvdaqian commented May 4, 2016

I have met the same issue in our production env.
I didn't run pm2 from a symlinked dir. But the issue is still there.

@dmr07
Copy link

dmr07 commented Aug 4, 2016

pm2 runs your main script inside a process container, which means if you run process.cwd(), the path will be /usr/local/lib/node_modules/pm2/lib/ProcessContainer.js (linux example) instead of /var/www/app/

One of the solutions is to use Node's native __dirname, which gets the current directory of your script file in question, and you can navigate your way from there i.e.

var path = __dirname.substring(0, __dirname.indexOf("routes")) + 'routeAwesome'

@lvdaqian
Copy link

lvdaqian commented Aug 5, 2016

A late update, I have found the reason of my issue.
I have added a script to remove the folder when deploy.
For the first time, it works fine. But for the second time the issue occurs.
kill and restart pm2, it works again.

So make sure you do not remove the work folder when pm2 is still on daemon. even though you recreate it again, pm2 still could not find the folder.

@aneeshmg
Copy link

aneeshmg commented Oct 6, 2017

I ran into the same issue, rebooting the machine fixed it for me...

@sfratini
Copy link

Just a FYI, my issue is that the build machine deleted the folder and pm2 cannot find it after that. A simple "pm2 update" solves it (Caution, it reloads all the current app list)

@aldee07
Copy link

aldee07 commented Mar 21, 2018

I ran into the same problem.

Following @lvdaqian 's answer, I remembered/realized that I deleted the directory and clone the project back with the same directory path prior to the pm2 start /path/to/my-pm2-config.json command.

So we can conclude that whenever the pm2 is running on daemon, the registry of previously started apps and the reference paths is still in its memory and that even the pm2 delete myapp command could not clear the reference path. To resolve, pm2 update works for me as suggested by @sfratini

Thanks

@danoctavian
Copy link

danoctavian commented Nov 19, 2018

i tried uninstalling/reinstalling pm2, stopping daemon, rebooting whole machine, no combination of anything works. i guess i can wipe out the entire HDD and start AGAIN. where do you store the state for this thing that it is so ridiculously persistent?

pm2 update doesn't do jack

can you bother to post at least ONE guide on how to solve this? THANKS

@sfratini
Copy link

@danoctavian In my experience, this issue is caused by either pm2 being run/installed by root so a normal app cannot run the pm2 executable, or that pm2 daemon was started from a directory that usually gets deleted or cleaned. Example, if you are inside a repo that gets cleaned and checkout by a build machine. After the cleanup, the build machine or script wont be able to find the directory.

So to me, the final solution was to kill every single daemon for pm2 and started from command line from a directory that I know won't be changed, like /var or /usr/yourUser. Make sure you start the daemon from that directory. After that, following commands run with pm2 should work, even the ones that involve reloading an app that was just installed/updated/checked out.

If that does not work, then double check the user than wants to run the command and the one that installed pm2. Maybe this is from a script that has less permissions than the one you are using or maybe you are doing "sudo npm i pm2" and then running the pm2 from a ctrontab. That I am guessing will cause problems.

@keyul-code
Copy link

I added PM2_HOME environment variable on a system level and now it works alright....

@UbaidillahGit
Copy link

I ran into this issue too. ** It finally worked after run ** pm2 kill
In my case, I first ran it in "fork" mode, works fine, and then changed to "cluster" mode, encounted this problem.
tried reinstalling pm2 with no luck;
tried run from the real dir (not the symlinked dir), with noluck.

thanks it's really work with pm2 kill !

@selimdev00
Copy link

Using sudo before starting any operation of PM2 worked for me :)

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