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.

- 6,939
- 6
- 35
- 38
4 Answers
This works for me:
nohup node server.js </dev/null

- 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
Another solution here is to run the command in a subshell using parentheses. (nohup node index.js)

- 339
- 2
- 4
- 16
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.

- 6,939
- 6
- 35
- 38
-
5Can you please share the complete command after prepending the global variable __dirname – Monti Chandra Jul 26 '18 at 05:35
-
-
@Xin I used the __dirname within the code of the server to retrieve the files. Not when starting the server through forever. – javabeangrinder Dec 02 '19 at 12:54
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

- 5,756
- 3
- 39
- 36