Compile one JavaScript file into an executable using nexe In our case, it'll create Windows executable file with the name “executable”. Double-tap on it or right-click and select the run command to start the application.
Some malware camouflages itself as node.exe which can be a security threat to your system. You can check if the file is located in the C:\Windows or C:\Windows\System32 folder, then it is a Trojan.
The solution I've used is Roger Wang's node-webkit.
This is a fantastic way to package nodejs apps and distribute them, it even gives you the option to "bundle" the whole app as a single executable. It supports windows, mac and linux.
Here are some docs on the various options for deploying node-webkit apps, but in a nutshell, you do the following:
Just as an added note - I've shipped several production box/install cd applications using this, and it's worked great. Same app runs on windows, mac, linux and over the web.
Update: the project name has changed to 'nw.js' and is properly located here: nw.js
There a few alternatives, both free and commercial. I haven't used any of them but in theory they should work:
Most will require you to keep the batch file as main executable, and then bundle node.exe and your scripts.
Depending on your script, you also have the option to port it to JSDB, which supports an easy way to create executables by simply appending resources to it.
A third quasi-solution is to keep node somewhere like C:\utils
and add this folder to your PATH
environment variable. Then you can create .bat files in that dir that run node + your preferred scripts - I got coffeescript's coffee
working on windows this way. This setup can be automated with a batch file, vb script or installer.
For anyone stumbling upon this question, there are now two projects that create exes out of your node projects, Pkg and Electron.atom.io, they differ slightly:
The best tool I know is NodeJS tool: zeit/pkg
It is very easy to use (much more than Nexe, just as an example), you can just install in globally:
npm install -g pkg
to create executables for macOS, Linux and Windows:
pkg exampleApp.js
I had a bit of complicated code which used NodeJS socket server, I tried different applications, none of them created it properly, except zeit/pkg.
By default, Windows associates .js
files with the Windows Script Host, Microsoft's stand-alone JS runtime engine. If you type script.js at a command prompt (or double-click a .js
file in Explorer), the script is executed by wscript.exe
.
This may be solving a local problem with a global setting, but you could associate .js
files with node.exe
instead, so that typing script.js at a command prompt or double-clicking/dragging items onto scripts will launch them with Node.
Of course, if—like me—you've associated .js
files with an editor so that double-clicking them opens up your favorite text editor, this suggestion won't do much good. You could also add a right-click menu entry of "Execute with Node" to .js
files, although this alternative doesn't solve your command-line needs.
The simplest solution is probably to just use a batch file – you don't have to have a copy of Node in the folder your script resides in. Just reference the Node executable absolutely:
"C:\Program Files (x86)\nodejs\node.exe" app.js %*
Another alternative is this very simple C# app which will start Node using its own filename + .js
as the script to run, and pass along any command line arguments.
class Program
{
static void Main(string[] args)
{
var info = System.Diagnostics.Process.GetCurrentProcess();
var proc = new System.Diagnostics.ProcessStartInfo(@"C:\Program Files (x86)\nodejs\node.exe", "\"" + info.ProcessName + ".js\" " + String.Join(" ", args));
proc.UseShellExecute = false;
System.Diagnostics.Process.Start(proc);
}
}
So if you name the resulting EXE "app.exe", you can type app arg1 ...
and Node will be started with the command line "app.js" arg1 ...
. Note the C# bootstrapper app will immediately exit, leaving Node in charge of the console window.
Since this is probably of relatively wide interest, I went ahead and made this available on GitHub, including the compiled exe if getting in to vans with strangers is your thing.
Haven't tried it, but nexe looks like nexe can do this:
https://github.com/crcn/nexe
Since this question has been answered, another solution has been launched.
https://github.com/appjs/appjs
At the time of this writing, this is the end-all solution for packaging node.js apps through a stripped down chromium package compiled into an executable.
Edit: AppJS is no longer active, but itself suggests a fork called deskshell.
https://github.com/sihorton/appjs-deskshell/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With