Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, September 26, 2016

How do I debug

How do I debug "Error: spawn ENOENT" on node.js?


When I get noticed with the following error:

events.js:72          throw er; // Unhandled 'error' event                ^  Error: spawn ENOENT      at errnoException (child_process.js:1000:11)      at Process.ChildProcess._handle.onexit (child_process.js:791:34)  

What procedure can I follow to fix it?

Author note: Lots of issues with this error encouraged me to post this question for future references.

Related questions on SO (scenarios that should be fixed with procedure)

Answer by laconbass for How do I debug "Error: spawn ENOENT" on node.js?


Step 1: Ensure spawn is called the right way

First, review the docs for child_process.spawn( command, args, options ):

Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.

The third argument is used to specify additional options, which defaults to:

{ cwd: undefined, env: process.env }

Use env to specify environment variables that will be visible to the new process, the default is process.env.

Ensure you are not putting any command line arguments in command and the whole spawn call is valid. Proceed to next step.

Step 2: Identify the Event Emitter that emits the error event

Search on your source code for each call to spawn, or child_process.spawn, i.e.

spawn('some-command', [ '--help' ]);  

and atach there an event listener for the 'error' event, so you get noticed the exact Event Emitter that is throwing it as 'Unhandled'. After debugging, that handler can be removed.

spawn('some-command', [ '--help' ])    .on('error', function( err ){ throw err })  ;  

Execute and you should get the file path and line number where your 'error' listener was registered. Something like:

/file/that/registers/the/error/listener.js:29        throw err;              ^  Error: spawn ENOENT      at errnoException (child_process.js:1000:11)      at Process.ChildProcess._handle.onexit (child_process.js:791:34)  

If the first two lines are still

events.js:72          throw er; // Unhandled 'error' event  

do this step again until they are not. You must identify the listener that emits the error before going on next step.

Step 3: Ensure the environment variable $PATH is set

There are two possible scenarios:

  1. You rely on the default spawn behaviour, so child process environment will be the same as process.env.
  2. You are explicity passing an env object to spawn on the options argument.

In both scenarios, you must inspect the PATH key on the environment object that the spawned child process will use.

Example for scenario 1

// inspect the PATH key on process.env  console.log( process.env.PATH );  spawn('some-command', ['--help']);  

Example for scenario 2

var env = getEnvKeyValuePairsSomeHow();  // inspect the PATH key on the env object  console.log( env.PATH );  spawn('some-command', ['--help'], { env: env });  

The absence of PATH (i.e., it's undefined) will cause spawn to emit the ENOENT error, as it will not be posible to locate any command unless it's an absolute path to the executable file.

When PATH is correctly set, proceed to next step. It should be a directory, or a list of directories. Last case is the usual.

Step 4: Ensure command exists on a directory of those defined in PATH

Spawn may emit the ENOENT error if the filename command (i.e, 'some-command') does not exist in at least one of the directories defined on PATH.

Locate the exact place of command. On most linux distributions, this can be done from a terminal with the which command. It will tell you the absolute path to the executable file (like above), or tell if it's not found.

Example usage of which and its output when a command is found

> which some-command  some-command is /usr/bin/some-command  

Example usage of which and its output when a command is not found

> which some-command  bash: type: some-command: not found  

miss-installed programs are the most common cause for a not found command. Refer to each command documentation if needed and install it.

When command is a simple script file ensure it's accesible from a directory on the PATH. If it's not, either move it to one or make a link to it.

Once you determine PATH is correctly set and command is accessible from it, you should be able to spawn your child process without spawn ENOENT being thrown.

Answer by Jiaji Zhou for How do I debug "Error: spawn ENOENT" on node.js?


I found a particular easy way to get the idea of the root cause of:

Error: spawn ENOENT  

The problem of this error is, there is really little information in the error message to tell you where the call site is, i.e. which executable/command is not found, especially when you have a large code base where there are a lot of spawn calls. On the other hand, if we know the exact command that cause the error then we can follow @laconbass' answer to fix the problem.

I found a very easy way to spot which command cause the problem rather than adding event listeners everywhere in your code as suggested in @laconbass' answer. The key idea is to wrap the original spawn call with a wrapper which prints the arguments send to the spawn call.

Here is the wrapper function, put it at the top of the index.js or whatever your server's starting script.

(function() {      var childProcess = require("child_process");      var oldSpawn = childProcess.spawn;      function mySpawn() {          console.log('spawn called');          console.log(arguments);          var result = oldSpawn.apply(this, arguments);          return result;      }      childProcess.spawn = mySpawn;  })();  

Then the next time you run your application, before the uncaught exception's message you will see something like that:

spawn called  { '0': 'hg',    '1': [],    '2':     { cwd: '/* omitted */',       env: { IP: '0.0.0.0' },       args: [] } }  

In this way you can easily know which command actually is executed and then you can find out why nodejs cannot find the executable to fix the problem.

Answer by PromInc for How do I debug "Error: spawn ENOENT" on node.js?


In my case, I was getting this error thrown due to the necessary dependent system resources not being installed.

More specifically, I have a NodeJS app that is utilizing ImageMagick. Despite having the npm package installed, the core Linux ImageMagick was not installed. I did an apt-get to install ImageMagick and after that all worked great!

Answer by Alex Turpin for How do I debug "Error: spawn ENOENT" on node.js?


For anyone who might stumble upon this, if all the other answers do not help and you are on Windows, know that there is currently a big issue with spawn on Windows and the PATHEXT environment variable that can cause certain calls to spawn to not work depending on how the target command is installed.

Answer by Nilzor for How do I debug "Error: spawn ENOENT" on node.js?


Windows solution: Replace spawn with node-cross-spawn. For instance like this at the beginning of your app.js:

(function() {      var childProcess = require("child_process");      childProcess.spawn = require('cross-spawn');  })();   

Answer by MTGradwell for How do I debug "Error: spawn ENOENT" on node.js?


I was getting this error when trying to debug a node.js program from within VS Code editor on a Debian Linux system. I noticed the same thing worked OK on Windows. The solutions previously given here weren't much help because I hadn't written any "spawn" commands. The offending code was presumably written by Microsoft and hidden under the hood of the VS Code program.

Next I noticed that node.js is called node on Windows but on Debian (and presumably on Debian-based systems such as Ubuntu) it's called nodejs. So I created an alias - from a root terminal, I ran

ln -s /usr/bin/nodejs /usr/local/bin/node

and this solved the problem. The same or a similar procedure will presumably work in other cases where your node.js is called nodejs but you're running a program which expects it to be called node, or vice-versa.

Answer by Alex Mills for How do I debug "Error: spawn ENOENT" on node.js?


@laconbass's answer helped me and is probably most correct.

I came here because I was using spawn incorrectly.

As a simple example:

this is incorrect:

    const s = cp.spawn('npm install -D suman', [], {          cwd: root      });  

this is incorrect:

    const s = cp.spawn('npm', ['install -D suman'], {          cwd: root      });  

this is correct:

   const s = cp.spawn('npm', ['install','-D','suman'], {          cwd: root      });  

Answer by chayasan for How do I debug "Error: spawn ENOENT" on node.js?


I got the same error for windows 8.The issue is because of an environment variable of your system path is missing . Add "C:\Windows\System32\" value to your system PATH variable.

Answer by Gum Joe for How do I debug "Error: spawn ENOENT" on node.js?


I ran into the same problem, but I found a simple way to fix it. It appears to be spawn() errors if the program has been added to the PATH by the user (e.g. normal system commands work).

To fix this, you can use the which module (npm install --save which):

// Require which and child_process  const which = require('which');  const spawn = require('child_process').spawn;  // Find npm in PATH  const npm = which.sync('npm');  // Execute  const noErrorSpawn = spawn(npm, ['install']);  

Answer by Leeroy Brun for How do I debug "Error: spawn ENOENT" on node.js?


As @DanielImfeld pointed it, ENOENT will be thrown if you specify "cwd" in the options, but the given directory does not exist.

Answer by vmit dhawan for How do I debug "Error: spawn ENOENT" on node.js?


Add C:\Windows\System32\ to the path environment variable.

Steps

  1. Go to my computer and properties

  2. Click on Advanced settings

  3. Then on Environment variables

  4. Select Path and then click on edit

  5. Paste the following if not already present: C:\Windows\System32\

  6. Close the command prompt

  7. Run the command that you wanted to run

Windows 8 Environment variables screenshot

Answer by Li Zheng for How do I debug "Error: spawn ENOENT" on node.js?


For ENOENT on Windows, https://github.com/nodejs/node-v0.x-archive/issues/2318#issuecomment-249355505 fix it.

e.g. replace spawn('npm', ['-v'], {stdio: 'inherit'}) with:

  • for all node.js version:

    spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['-v'], {stdio: 'inherit'})  
  • for node.js 5.x and later:

    spawn('npm', ['-v'], {stdio: 'inherit', shell: true})  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.