15

I have a problem with node.js running a small web server serving files from the file system. When starting it with node server.js it works like a charm but when starting it with nohup or forever node.js can't find the files.

javabeangrinder
  • 6,939
  • 6
  • 35
  • 38

4 Answers4

16

This works for me:

nohup node server.js </dev/null
forzagreen
  • 2,509
  • 30
  • 38
  • Whoa, I have no idea why, but this works. `< /dev/null` disables manual input to the nohup command, but how this is related to file path, I don't know. I was personnaly using supervisor and not forever, but the error was the same. I'll add that it wasn't necessary until an update in supervisor made it listen to manual input. – Louis Ameline May 02 '17 at 13:46
  • This is the correct answer. I was facing the same issue - executing directly `node file.js` worked fine, doing it using `nohup` did not work and I got the EBADF error mentioned above. putting here for anyone that might need it, here is the full command I use to also log the output and errors: `nohup node file.js scriptresults.log 2> scripterror.log &` – Lior Gross Oct 28 '21 at 17:27
4

Another solution here is to run the command in a subshell using parentheses. (nohup node index.js)

Jk Jensen
  • 339
  • 2
  • 4
  • 16
2

It turned out to be the file path of the file that was the problem. When running the server using node the working directory is the same as the server.js file thus node.js manages to find the file.

When starting whilst using nohup or just starting with forever the working directory doesn't seem to be the same as server.js.

I solved this by prepending the global variable __dirname to the filename.

javabeangrinder
  • 6,939
  • 6
  • 35
  • 38
0

I've got here trying to fix an issue which appears from time to time on windows while having some nodejs and python scripts calling each other.

The solution was to unset NODE_CHANNEL_FD environment variable before calling node or npm from python code

source for solution

Ohad Cohen
  • 5,756
  • 3
  • 39
  • 36