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.
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.
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 yourPATH
environment variable. Then you can create .bat files in that dir that run node + your preferred scripts - I got coffeescript'scoffee
working on windows this way. This setup can be automated with a batch file, vb script or installer.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 bywscript.exe
.This may be solving a local problem with a global setting, but you could associate
.js
files withnode.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:
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.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
Trydisclose
: https://github.com/pmq20/disclosedisclose
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 foreverTry
node-compiler
: https://github.com/pmq20/node-compilerI 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.
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