user2799015
user2799015

Reputation: 1

Node.js error. Cannot understand

I am getting this error while running a simple node program:

/home/ubuntu/parent.js:4
    throw error;
      ^
Error: Command failed: /bin/sh: 1: node: not found

at ChildProcess.exithandler (child_process.js:637:15)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:735:16)
at Socket.<anonymous> (child_process.js:948:11)
at Socket.EventEmitter.emit (events.js:95:17)
at Pipe.close (net.js:466:12)

parent.js :

 var exec = require('child_process').exec; 
 exec('node child.js',{env: {number: 123}},function(error,stdout,stderr){ 
   if(error){ throw error; } console.log('stdout:\t',stdout); 
   console.log('stderr:\t',stderr); 
 });

child.js :

var number = process.env.number; 
console.log(typeof(number));

Upvotes: 0

Views: 2795

Answers (2)

Aaron Dufour
Aaron Dufour

Reputation: 17505

When you pass these options:

{env: {number: 123}}

you're overwriting all of the environment variables, so it doesn't inherit your current ones. This includes the PATH, which is required for it to find node. You'll need to copy the current environment variables in addition to the one you want:

env = {};
for(key in process.env) {
  env[key] = process.env[key];
}
env.number = 123;

And then you use this env as the env option:

{env: env}

Upvotes: 3

dansch
dansch

Reputation: 6267

So, you have multiple node instances you're trying to run. To me, this doesn't look right. You can require('child.js') or build your application better.

If you really want to do this, you need to understand paths. The parent node application is running as a user, and once ran by that user, it doesn't seem to have the path to the original node file. try exec'ing the command export PATH=$PATH:/usr/local/bin where /usr/local/bin/node exists. If node is in /bin, then use that. Find where the node executable is and add that bin directory to the path. This has to be done on the user that node runs as.

In node, you might be able to see what PATH is available by doing

exec('echo $PATH')

each directory it's checking for is separated by colons

It would be preferable to do this outside of node, but to me this whole situation seems like it should be rethought from first concepts.

Upvotes: 0

Related Questions