setting environment variables for node.js child process - node.js

I have a node webkit app, part of which involves using child processes in node to call pdftk (a separate command line program).
I don't want my users to have to install pdftk or use the command line, so I included pdftk in the packaged version of the node webkit app. If I run this packaged app from the command line, it works fine - and I assume that's because it's using the version of pdftk that's installed on my computer, not the one packaged with the app.
When I try to launch the app by double clicking on an icon in the gui, as I'd want a user to be able to do, I get a node.js error - child process ENOENT. I think that's because when launched through the gui, it doesn't inherit the environment variables (including PATH) from my command-line environment.
I know I can set environment variables as an option when I call the child process, but haven't been able to figure out how to do that correctly. I'm not sure what variable I should set, or what I should set it to. I suppose I'm not sure if it's even possible to call pdftk from within the packaged app, or if I would need to have the user install it on their own computer. Any help would be much appreciated.

I think this might not be about environment variables. I guess when you run from the command line your current working directory (CWD) is where the app is. And I think you set the path to pdftk relative to the node script. When you double click (a shortcut) the current working directory and the path where the node script is located are different so relative paths don't work as expected.
When you use relative paths, always use __dirname to get the path the node script is on and use it to set path to the pdftk file. The path.resolve function can be useful when doing this. Read path.resolve documentation
I strongly recommend you to check this question and answers
How do I get the path to the current script with Node.js?
What is the difference between __dirname and ./ in node.js?

Related

How to start a Nodejs server with options?

My computer runs Windows10 Enterprise.
I found this repo for creating a Nodejs server for tchatbot. As you can see there are options for starting the server. I tried to execute this command : node app.js DF_PROJECT_ID="agent-human-handoff-sampl-jseo" DF_SERVICE_ACCOUNT_PATH="D:\Docs\TchatBot\clé_account_service_agent_human_operator\agent-human-handoff-sampl-jseo-3349b2f01974.json"
But I got error : You need to specify a path to a service account keypair in environment variable DF_SERVICE_ACCOUNT_PATH
So what is wrong ?
It's basically same as jfriend00's solution, but I add node app.js in the end. And you just follow below sequence to run command.
set DF_SERVICE_ACCOUNT_PATH="D:\Docs\TchatBot\clé_account_service_agent_human_operator\agent-human-handoff-sampl-jseo-3349b2f01974.json"
set DF_PROJECT_ID="agent-human-handoff-sampl-jseo"
node app.js
By the way, if you use linux system or macOS, you'll use following command to start server.
(Just one line)
DF_SERVICE_ACCOUNT_PATH="D:\Docs\TchatBot\clé_account_service_agent_human_operator\agent-human-handoff-sampl-jseo-3349b2f01974.json" DF_PROJECT_ID="agent-human-handoff-sampl-jseo" node app.js
You can just set these in the environment in a command shell before running nodejs from that command shell:
set DF_SERVICE_ACCOUNT_PATH="D:\Docs\TchatBot\clé_account_service_agent_human_operator\agent-human-handoff-sampl-jseo-3349b2f01974.json"
set DF_PROJECT_ID="agent-human-handoff-sampl-jseo"
Then, you you can run your program and these variables will be in the environment that your node program inherits. If you want to automate this, you can create a small batch file that will set them and then run your program. Keep in mind that setting environment variables like this sets them on for programs run from the current command shell, not other command shells and not for programs run other ways.
After setting those, your environment is now configured and you would run your program just as always:
node app.js

NodeJS installation to jenkins : npm not found

Maybe someone can tell me what I missed installing nodeJS to jenkins, because when I want to execute shell script npm update I get error npm: not found.
I installed jenkins nodejs plugin then added installer:
After I created job with prams:
What I need to do more, to have accessible npm in any jenkins job?
It works for me. The only difference is that where you have a text box that reads "0.12.7", I have a popup menu that reads "Node.js 0.12.7". Maybe something has gone wrong and you should delete the installer and add it back?
Windows users, did you restart?
Do keep in mind that you need to restart active processes after installing npm.
During installation the folder of your npm.cmd is added to the PATH variable. However, active applications don't refresh their PATH variable automatically. That also holds for your active Jenkins service (and its child processes). They are still running with an old version of your PATH variable.
In other words, you need to restart the Jenkins service (or restart your entire system). Once restarted, Jenkins will have an updated PATH variable, and it will be able to find the npm.cmd processes and others.
What's wrong with using absolute paths?
Using the full path is not a solid solution. A package.json file can contain references to other cli processes (e.g. node, ng, react-scripts, npm-run-all, ...). You will probably have similar issues there. There is no way to keep up and reference all of them with full paths.

How can I run a node.js server

I'm new to node and have many things unclear.
Like, for php, I just need a index.php file on server's root dir and it can work by itself.
However, for a node.js file, we need to "node" command it in terminal right?
So what if we close that terminal? How can I keep it running to accept my requests?
You are correct in saying that the 'node' command will start a node process with whatever script you supply to it.
As far as keeping it running, there are several ways to do it. There are plenty of CLI libraries that will help you. For example, this one is called Forever
If you are using linux, you can simply run the node process as a background task:
node server.js &
To run node without terminal, you might want to check out one of these modules depending on your platform:
node-mac
node-windows
node-linux

Running node.js code just displays a node identifier

I have the following code in a file called server.js.
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
I use the command prompt and naviage to the folder where the file recides and then the run the command
node server.js
But I don't get the expected output. Instead I get
The node identifier for {My Machine Name} is v2hrfnqaj.
Note: I already have node installed in my machine and it was working fine.
Was getting this when I was trying to run cordova commands. Steps to resolve:
Windows
In CMD prompt, type "where node". As Michael mentioned, this shows
you the likely culprit, that you have 2 nodejs EXEs installed on
your machine.
Navigate to Start > Computer > Right-click Properties > Advanced system settings
Under the Advanced tab, select Environment Variables
Under System variables, select "Path" variable
Find nodejs EXE, usually "C:\Program Files (x86)\nodejs\"
Cut and paste this to the beginning of the "Path" variable. Ensure
the paths are separated by a ";"
Open a new CMD prompt and try cordova again
This happens when Harvest SCM is installed on your system. It has an executable with the name node.exe at <Program Files (x86)>\CA\SharedComponents\PEC\bin (where <Program Files (x86)> is your x86 program files folder). This path is present in your PATH variable before the path to Node.js's node.exe.
Update: You don't need the elaborate scheme listed in the old answer. You just have to open the Command Prompt and run:
C:\> nodevars
nodevars.bat is a small script that does essentially the same thing described below (but in a safer way). If you have node installed, this script should be in path. (If not make sure to add C:\Program Files\nodejs to your path. But make sure to append it in the end so Harvest SCM does not break).
Everything below is outdated, but I will leave it for the curious reader.
You can do either of following two things you can do to overcome this problem:
Remove <Program Files (x86)>\CA\SharedComponents\PEC\bin from PATH environment variable.
Add/move <Program Files (x86)>\nodejs to the beginning of the PATH environment variable (This is the currently accepted answer from djrpascu).
You can do better!
There are two problems with the above approaches:
You break Harvest SCM's functionality.
If you do not have elevated privileges to change PATH, you are out of options. (Thanks #Glats)
So I created this little batch file, and put it in a directory where I have several other personal scripts (this directory is in my PATH). Here's the gist for the script.
nodecmd.bat
#echo off
set path=%path:C:\Program Files (x86)\CA\SharedComponents\PEC\bin;=%;C:\Program Files (x86)\nodejs;
start %ComSpec%
Then the next time you want to run Node.js, instead of Command Prompt, you open the new script with "Run..." command.
Windows+R
nodecmd
A command prompt will appear. You can use this command prompt to run node without a hassle.
Explanation
This bit deletes the Harvest's executable's path from PATH variable:
%path:C:\Program Files (x86)\CA\SharedComponents\PEC\bin;=%;
And this adds the Node.js's path:
set path=...;C:\Program Files (x86)\nodejs;
The result is a string that contains the original PATH variable minus Harvest's path, plus Node's path. And it is set as PATH variable in the scope of current batch file.
Note: You might have to change the path's in the script to suit software installation folders in your system).
Next line, start %ComSpec% starts a Command Prompt. By this time, the PATH variabe is modified. With modified environment variables, you can run node within this new Command Prompt. The environment variable modification does not affect the rest of the system, making sure that Harvest SCM software runs without breaking.
Don't break your Harvest SCM by removing it from path. Try this one, open your windows command line (cmd) and then pass the following nodejs batch file so that it will set your command line to nodejs environment. Enjoy the node commands there.
C:> "C:\Program Files\nodejs\nodevars.bat"
You can also prioritize in the environments.
Steps:
Computer -> Right click -> Properties -> Advanced system settings -> Environment variables -> PATH(in system variables list) -> Edit -> Prioritize by moving up
This is old, but I ran into this same problem. Exact same message (with my machine name of course). The issue was that there was another node executable on the path, in C:\Program Files (x86)\CA\SharedComponents\PEC\bin. I'm on a windows machine, so running where node showed the two conflicting "node" executables in the path.
To fix the problem, I just removed the CA directory from the PATH environment variable.
I faced the same problem and simply changed the the name of node.exe file from Harvest. This hasn't broken anything from Harvest and I can keep working with it.
Change the Harvest's command name to node_.exe:
ren "C:\Program Files (x86)\CA\SharedComponents\PEC\bin\node.exe" "C:\Program Files (x86)\CA\SharedComponents\PEC\bin\node_.exe"
I think you're running the wrong node command.
Try locating or re-downloading your nodejs installation and add it to your path as the first directory. If you're running linux or unix you can try 'which node' to see what is being run.
Note that in some cases, the node.js executable is called nodejs so you may want to try
nodejs server.js as well
I used the node.js command prompt, instead of the windows default command prompt and it worked for me. Did not know why it did't work in the windows default command prompt.
I was also running with same issue - while defining the path for windows use below parameter
Windows:
set NODE_PATH=C:\nodejs
OR
Set the environment variable for nodejs
NODE_PATH=C:\nodejs
Path= C:\nodejs
(append the path contain this string “c:\nodejs”)

Run a node.js server from Geany

A simple question: Is it possible to configure the Geany IDE so that Node.js servers can be run directly from Geany using the "Run" button?
When inside a JS file, go to Build > Set Build Commands, there should be a section title Execute commands. To use node to execute your files, put: node "%f" in the "Execute" command textbox.
When you change this, any .js files you are editing will run node in the virtual terminal when you hit F5.
If you want to set up an entire project to run the server whenever you're working somewhere within a given directory structure, you'll have to mess with project-level configuration. (something I don't usually bother with) My solution here just gives you a quick way to execute a single JS file without using an external terminal.
UPDATE: node "%f" seems to be legacy, but nodejs "%f" works

Resources