'How to make exe files from a node.js app?
I have a node app that I wrote, that I run as follows:
node.exe app.js inputArg
Is there some way I can package this into a .exe by itself? So I can just do something like this?
App.exe inputArg
I have some way of faking this by using a batch file, so I can do this:
App.bat inputArg
But this requires that I have all the dependencies and node in that folder, which is not ideal.
Solution 1:[1]
There a few alternatives, both free and commercial. I haven't used any of them but in theory they should work:
- Iexpress (native windows tool)
- Quick Batch File Compiler (commercial)
- BoxedApp Packer
- "Advanced" Batch To EXE Converter" (freeware)
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.
Solution 2:[2]
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:
- Zip up all your files, with a package.json in the root
- Change the extension from .zip to .nw
- copy /b nw.exe+app.nw app.exe
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
Solution 3:[3]
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:
- Pkg will compile your project to native code, they also include assets AND a NodeJS installation (the system won't need to install NodeJS to run the executable). Your source code will not be included.
The resulting executable is Windows ONLY ( .exe ).All platforms are supported now.It now requires a licence for commercial products.Fully open source. - Electron.atom.io while it will not compact and generate an executable for you, it CAN be used to distribute NodeJS apps since they offer a way to distribute applications. The benefits are that electron is multi-platform (Windows, macOS, Linux), the cons are that your source code WILL be included, even though they offer a way to distribute your app inside an asar archive. It will not be bulletproof but it's still better than your source in plain.
Solution 4:[4]
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.
Solution 5:[5]
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.
Solution 6:[6]
Haven't tried it, but nexe looks like nexe can do this:
Solution 7:[7]
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.
Solution 8:[8]
There are a lot of good answers here, but they're not all as straightforward as JXcore.
Once you have JXcore installed on windows, all you have to do is run:
jx package app.js "myAppName" -native
This will produce a .exe file that you can distribute and can be executed without any external dependencies whatsoever (you don't even need JXcore nor Node.js on the system).
Here's the documentation on that functionality: http://jxcore.com/packaging-code-protection/#cat-74
Edit 2018
That project is now dead but it is still hosted here: https://github.com/jxcore/jxcore-release (thanks @Elmue)
Solution 9:[9]
Try disclose
: https://github.com/pmq20/disclose
disclose
essentially makes a self-extracting exe out of your Node.js project and Node.js interpreter with the following characteristics,
No GUI. Pure CLI.No run-time dependenciesSupports both Windows and UnixRuns slowly for the first time (extracting to a cache dir), then fast forever
Try node-compiler
: https://github.com/pmq20/node-compiler
I have made a new project called node-compiler
to compile your Node.js project into one single executable.
It is better than disclose
in that it never runs slowly for the first time, since your source code is compiled together with Node.js interpreter, just like the standard Node.js libraries.
Additionally, it redirect file and directory requests transparently to the memory instead of to the file system at runtime. So that no source code is required to run the compiled product.
How it works: https://speakerdeck.com/pmq20/node-dot-js-compiler-compiling-your-node-dot-js-application-into-a-single-executable
Comparing with Similar Projects,
pkg(https://github.com/zeit/pkg): Pkg hacked fs.* API's dynamically in order to access in-package files, whereas Node.js Compiler leaves them alone and instead works on a deeper level via libsquash. Pkg uses JSON to store in-package files while Node.js Compiler uses the more sophisticated and widely used SquashFS as its data structure.
EncloseJS(http://enclosejs.com/): EncloseJS restricts access to in-package files to only five fs.* API's, whereas Node.js Compiler supports all fs.* API's. EncloseJS is proprietary licensed and charges money when used while Node.js Compiler is MIT-licensed and users are both free to use it and free to modify it.
Nexe(https://github.com/nexe/nexe): Nexe does not support dynamic require because of its use of browserify, whereas Node.js Compiler supports all kinds of require including require.resolve.
asar(https://github.com/electron/asar): Asar uses JSON to store files' information while Node.js Compiler uses SquashFS. Asar keeps the code archive and the executable separate while Node.js Compiler links all JavaScript source code together with the Node.js virtual machine and generates a single executable as the final product.
AppImage(http://appimage.org/): AppImage supports only Linux with a kernel that supports SquashFS, while Node.js Compiler supports all three platforms of Linux, macOS and Windows, meanwhile without any special feature requirements from the kernel.
Solution 10:[10]
I'm recommending you BoxedApp for that. It is a commercial product that working very well. It works for any NodeJS app. The end-user will get just one EXE file for your app. No need for installation.
In Electron, for example, the end user needs to install/uncompress your app, and he will see all the source files of your code.
BoxedApp It is packaging all the files and dependencies that your NodeJS app needs. It supports compressions, and the compiled file works on Windows XP+
When you use it, be sure to add Node.EXE to the compiled app. Choose a node version that supports Windows XP (If you need this)
The program supports command line arguments, So after package, you can do
c:\myApp argument1
And you can get the argument in your node code:
process.argv[1]
Sometimes your app has files dependencies, so in BoxedApp you can add any folder and file as a dependency, for example, you can insert a settings file.
var mySettings=require('fs').readFileSync('./settings.jgon').toString()
More info:
Just a note: usually I'm recommending about open-source/free software, but in this case I didn't found anything that gets the job done.
Solution 11:[11]
Try nexe which creates a single executable out of your node.js apps
Solution 12:[12]
I did find any of these solutions met my requirements, so made my own version of node called node2exe that does this. It's available from https://github.com/areve/node2exe
Solution 13:[13]
I've been exploring this topic for some days and here is what I found. Options fall into two categories:
If you want to build a desktop app the best options are:
1- NW.js: lets you call all Node.js modules directly from DOM and enables a new way of writing applications with all Web technologies.
2- Electron: Build cross platform desktop apps with JavaScript, HTML, and CSS
Here is a good comparison between them: NW.js & Electron Compared. I think NW.js is better and it also provides an application to compile JS files. There are also some standalone executable and installer builders like Enigma Virtual Box. They both contain an embedded version of Chrome which is unnecessary for server apps.
if you want to package a server app these are the best options:
node-compiler: Ahead-of-time (AOT) Compiler designed for Node.js, that just works.
Nexe: create a single executable out of your node.js apps
In this category, I believe node-compiler is better which supports dynamic require and native node modules. It's very easy to use and the output starts at 25MB. You can read a full comparison with other solutions in Node Compiler page. I didn't read much about Nexe, but for now, it seems Node Compiler doesn't compile the js file to binary format using V8 snapshot feature but it's planned for version 2. It's also going to have built-in installer builder.
Solution 14:[14]
I was using below technology:
- @vercel/ncc (this make sure we bundle all necessary dependency into single file)
- pkg (this to make exe file)
Let do below:
npm i -g @vercel/ncc
ncc build app.ts -o dist (my entry file is app.ts, output is in dist folder, make sure you run in folder where package.json and app.ts reside, after run above you may see the index.js file in the folder dist)
npm install -g pkg (installing pkg)
pkg index.js (make sure you are in the dist folder above)
Solution 15:[15]
Got tired of starting on win from command prompt then I ran across this as well. Slightly improved ver. over what josh3736. This uses an XML file to grab a few settings. For example the path to Node.exe
as well as the file to start in the default app.js. Also the environment to load (production, dev etc) that you have specified in your app.js (or server.js or whatever you called it). Essentially it adds the NODE_ENV={0}
where {0} is the name of your configuration in app.js as a var for you. You do this by modifying the "mode" element in the config.xml. You can grab the project here ==> github. Note in the Post Build events you can modify the copy paths to auto copy over your config.xml and the executable to your Nodejs
directory, just to save a step. Otherwise edit these out or your build will throw a warning.
var startInfo = new ProcessStartInfo();
startInfo.FileName = nodepath;
startInfo.Arguments = apppath;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
if(env.Length > 0)
startInfo.EnvironmentVariables.Add("NODE_ENV", env);
try
{
using (Process p = Process.Start(startInfo))
{
p.WaitForExit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Start Error", MessageBoxButtons.OK);
}
Solution 16:[16]
There may be many other options but to my knowledge, there is one project in active development on GitHub https://github.com/zeit/pkg. You can play with it. One more at https://github.com/nexe/nexe but not in active development.
Solution 17:[17]
Currently, I think the way to go is deno
This is
deno compile mainProgram.js
Existing node projects may need some sort of conversion, at least for require statements (convert node projects to deno)
Solution 18:[18]
There is an opensource build tool project called nodebob. I assume that working platform windows,
- simply clone or download project
- copy all node-webkit project files of yours into the app folder.
- run the build.bat script.
You will find the executable file inside the release folder.
Solution 19:[19]
Note: You need python and pip (maybe easy_install or dpkg on debian) as well for this to work.
Create a node virtual environment using nodeenv (install using python -m pip install nodeenv
) (make using python -m nodeenv envname
)
Create a .bat file launching the code using the node virtual environment's exe file (stored in ./envname/Scripts/node.exe
),
then use some bat to exe converter (http://www.battoexeconverter.com/) idk
then distribute all the files except the bat file use the exe converted file instead by making them into a installer using some program. There you go.
Instead of nodeenv, you could use NVM or the official node installer.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow