node.js does not respect all paths in $PATH variable - node.js

This question is related to
Nodejs Child Process with Unix Executable.
However I could not find a good solution and thanks to my reputation points, I am unable to post a comment there.
To explain my problem, I have some of my own executables which I need to execute from node.js. The path for these executables is already set in the PATH variable. Hence, I can run my executables without any path-prefix from Terminal.
Inspite of that, node.js receives process.env.PATH set to following value:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
I tried appending the path to it before spawning the process:
process.env.PATH = process.env.PATH + ":<path_to_my_executables>"
but the process exits with 127 Shell code which is "command not found" if I correctly remember.
Any ideas?
Thank you in advance!

Okay, I got it work using a shell script. Definitely there must be a better solution to this.
I exported the required paths to my executable (and its dependencies) in the shell script first and then started execution. Hence the shell script structure is something like:
export JAVA_HOME=<java_path>
export EXECUTABLES_HOME=<executables_path>
export PATH="$PATH:$JAVA_HOME/bin:$EXECUTABLES_HOME"
<execute_required_executable> <arguments>
If someone has a better solution, please do let me know!

Related

environment variable LD_LIBRARY_PATH is not working until I reassign it

I have a strange behavior on an embedded linux system. It is created from an image file and has been used on many system for about a year without any issues. But a new system that I installed just now fail to work.
I have an app that is started by one of the boot scripts, and is dependent on a library foo.so in a special path.
So the script sets LD_LIBRARY_PATH to foopath.
LD_LIBRARY_PATH=foopath \
foo.app
But after boot I can see that the app was never started and if I try to start it manually it complains that it can't find the library.
The strange thing is that I can get it to work by setting the same path again by just copying the path using the clipboard, like this:
$ echo $LD_LIBRARY_PATH
foopath
$ export LD_LIBRARY_PATH=foopath
After this my app will find the library and executes correct.
But if I reboot the system the problem is of course back again.
What could be wrong, and what can I do to solve the issue?
As seen in the comments I got it to work by changing my script like this, but I still can't understand why it works on some systems, but not all.
export LD_LIBRARY_PATH=foopath
foo.app

how to control where spawnCommand method executes

How can I understand and control from where spawnCommand runs?
I am creating a yo generator and as part of the process I need to call an external tool. The way I am attmepting to do this is by calling a powershell script from spawnCommand. I was having trouble with spawnCommand having spaces in paths, so I just encapsulated that within the powershell file. I saw something about superspawn but couldn't get it to work.
Anyway, my main problem is that I am trying to call powershell and give it the script file as an argument, but it can't find the script file. Here is the code:
signatureKeyFile: function() {
var done = this.async();
this.spawnCommand('powershell', ['sign.ps1'], {
cwd: this.destinationPath('.')
}).on('close', done);
},
I tried using procmon to see what directory it executes from and understand it better. It looks like it tries it in a ton of places, but seemingly all environment paths. It doesn't try the source or destination path for the yo generator, or at least that's how it seemed to me.
So my questions are
1) which working directory does spawnCommand run from by default?
2) How can I contorl that so it can find the this powershell file? spawnCommand won't let me use spaces in paths, so I can't feed it a full path or so it seems to me.
Thanks so much!
spawnCommand won't let me use spaces in paths, so I can't feed it a full path or so it seems to me.
That's usually a well known issues of command line. Arguments are separated by spaces, so it just assume your path is actually multiples arguments.
I'm not 100% for windows, but on OSx, you'd espace spaces as some\ path\ with\ spaces.
Another options is to pass arguments as strings. What if you were to wrap your path in double quotes?
this.spawnCommand('powershell', ['"/home/My Home/sign.ps1"']);
FWIW, Yeoman spawnCommand is just a wrapper for Node spawn command where we help normalize a command so it'll run fine on windows. See
https://github.com/yeoman/generator/blob/master/lib/actions/spawn-command.js#L20
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
It'll help you to test the correct syntax to pass path if you can reduce the scope of your problem to only the Node spawn command.
[Edited to fix broken link]
We were able to solve it by setting the current working directory or cwd, with the spaces:
this.spawnCommandSync('sn.exe', ['-k', format("{0}\\{1}.snk", this.destinationPath('.'), this.props.myItemName)], {
cwd: 'C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\NETFX 4.6 Tools'
});

Linux shell commands not found even though their paths are listed in the PATH variable

The google app engine cli commands cannot be found in the python sdk. I've already checked the google_appengine file. The shell commands are there, and I already added the app engine file path to the PATH variable. I echo the PATH variable and the directory to the app_engine file shows up, but still shows not found when I try to use a command that's inside the file. If I reference the command directly by using its whole path, the command works, but otherwise it won't. Is there anything else that needs to be done to reference a command in shell?
More error info please,
If I reference the command directly by using its whole path, the command works
I think your python path maybe wrong, Python 2.6 is not supported.
so are you have mulit-version python?
Thank you for your contributions everyone. It turns out the path I was using was wrong. Instead of /home/Programs/Apps/google_appengine it should have been /home/Programs/google_appengine. I guess the shell doesn't check to see if the path you add to the PATH variable actually exists. Who knew? Not me
Until I run into another problem stack overflow--which may be soon
See ya

Node-Webkit Child Process Exec

I want to execute a homebrew command for example
brew list
I followed the documentation and executed it like this:
child = exec('brew', function (error, stdout, stderr) {
console.log(stdout);
console.log(stderr);
});
I am getting a command not found error, and realized that if I do /usr/local/bin/brew as the command it works. However simply using 'brew' should work as well since I can run 'brew' from the command line just as such.
Why is this the case and what does it take to make 'brew' run as a child process in node? I have a feeling part of the issue because the command on node-webkit seems to execute from bin/sh.
Thanks
It may depend on how you're starting node-webkit and how you're setting your PATH. When I start from the command line, it inherits the environment variables from my command-line environment, including PATH. If I start by double clicking in a gui, it inherits from the system (presumably /etc/paths), and any additions I make in my .bashrc/.bash_profile have no effect.
Also, I'm no security expert, but my understanding of best practices would include using an absolute path to the executable you're running, so it's harder to spoof by setting an environment variable. By that measure, you're better off using the full path to brew anyway.

customed command not found in a new terminal

everyone.
I had a basic question want to consult, about the environment variable setting.
After closed my one existed terminal which could execute compile(make) and do customed(mksdboot) command, i can't do mksdboot command anymore(I had execute a predefined setting environment variable shell script i.e. $ . ./arndale_envsetup.sh again) in the new terminal.
Cause i am a beginner in Linux, i am not very clearly about the environment variable setting rules.
i had tried to 'su' or 'sudo' to execute mksdboot, but no luck:(
ps. I had another project needs to compile in my PC(i didn't export PATH to .bashrc, only execute export PATH when i open a new terminal every time), may it efforts the original project's environment variable?
thanks.
[UPDATED]
i tried using $source ./arndale_envsetup.sh, relative commands worked finally.
but i still did't figure out the reason between work or not work. >"<
The command
history
will list what your previous commands where.
This might give you a pointer what set the path in the way you needed it.
You could also try to see where you command is via
locate mksdboot

Resources