Search code examples
node.jsenoent

NodeJS: ENOENT error on calling a shell script


Below is the error I am receiving when attempting to run a shell script located at /var/www/nodejs/restart-server.sh.

exec error: { Error: spawn sh /var/www/nodejs/restart-server.sh ENOENT
    at _errnoException (util.js:1022:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
    at onErrorNT (internal/child_process.js:372:16)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn sh /var/www/nodejs/restart-server.sh',
  path: 'sh /var/www/nodejs/restart-server.sh',
  spawnargs: [],
  cmd: 'sh /var/www/nodejs/restart-server.sh' }

The path is from root (/) on purpose. If I execute this sh /var/www/nodejs/restart-server.sh while not running NodeJS, it works. But NodeJS cannot seem to run it.

Most answers I've found say that it is because of an incorrect file path, or because they are calling a relative path as if it is a root path. But I am calling a root path purposely, and the path is the correct root path. I cannot find any other answers for this.

Code

var exec = require('child_process').execFile;

var restartScript = exec('sh /var/www/nodejs/restart-server.sh',
    (error, stdout, stderr) => {
        console.log(stdout);
        console.log(stderr);
        if (error !== null) {
            console.log('exec error:', error);
        }
    });

Problem Found

I forgot to change .execFile to just .exec


Solution

  • I forgot to change .execFile to just .exec