How to make exe files from a node.js app?
Asked Answered
D

22

178

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.

Darnelldarner answered 17/11, 2011 at 19:41 Comment(5)
Possible dupe: #6146061Composure
This appears to be part of a Duplicate Pool: #6146061, #7557864, https://mcmap.net/q/77051/-how-to-make-exe-files-from-a-node-js-app, #8794640, #9725317, #13388608Chishima
A good list of tools is here: https://mcmap.net/q/77054/-packaging-a-node-js-web-application-as-a-normal-desktop-application-closedKnot
I think anyone adding answer here really needs to consider if it's already been done. This question is turning into a pile of links to various open source products of varying quality/activity.Aposiopesis
@Aposiopesis A good description of SO :)Abort
N
47

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.

Ninos answered 18/11, 2011 at 1:12 Comment(3)
I tried the "Advanced" Batch To EXE converter, and it worked like a charm. You can package the dependencies into the EXE file. Thanks for the help.Darnelldarner
@Aishwar, but you can't bundle node_modules folder, so you can't do anything advanced in it...Carabiniere
What if you zip/rar node_modules, and extract to a temp directory when the application runs? Could get a bit slow, if you've got a lot of modules, though.Nollie
R
80

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:

  1. Zip up all your files, with a package.json in the root
  2. Change the extension from .zip to .nw
  3. 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

Regretful answered 6/3, 2014 at 15:10 Comment(6)
Just tried it, worked great! And it's supported by people at Intel.Hausmann
This should be the correct and most current answer as of today. See also the node webkit cheatsheet here: gist.github.com/LeCoupa/80eca2716a2b13c37cce Includes other intensely useful stuff like how to compile the JS (or at least crunch down to byte code depending on how old-fashioned you want to be about the word "compile").Douglasdouglashome
I like this answer, however this still does not create a standalone exe. According to Intel and nw.js at Github you still need to have at least two other files in the same folder: nw.pak and icudt.dll. How do you create a stand-alone exe?Pedo
@JeffBaker if you look down a bit in the docs on github, you can see this: github.com/nwjs/nw.js/wiki/… which you can do if you don't want to have to include additional files.Regretful
@Clayton, Nw.js is bouched by chrome.com as well. developer.chrome.com/apps/migration#nativeWasserman
Not sure if this is still a good approach in 2019. It sounds like it would bundle in the large WebKit library which is fine for HTML GUI apps but unnecessary for console apps.Paniculate
N
47

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.

Ninos answered 18/11, 2011 at 1:12 Comment(3)
I tried the "Advanced" Batch To EXE converter, and it worked like a charm. You can package the dependencies into the EXE file. Thanks for the help.Darnelldarner
@Aishwar, but you can't bundle node_modules folder, so you can't do anything advanced in it...Carabiniere
What if you zip/rar node_modules, and extract to a temp directory when the application runs? Could get a bit slow, if you've got a lot of modules, though.Nollie
E
44

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:

  1. 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.
  2. 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.
Entelechy answered 27/10, 2015 at 9:51 Comment(6)
Please update: EncloseJS DOES support Linux and OS X now.Exotic
Edited comment! @ExoticEntelechy
EncloseJS is the way to go. I'm going to be trying it right now! Thanks for adding your answer! +1 :)Izettaizhevsk
I cannot install EncloseJS. It says Err code ELIFECYCLE without any usefull error message.Wendy
EncloseJS is not maintained any more. The heir appears to be zeit/pkg (npmjs.com/package/pkg)Corregidor
asar is just an archive. See https://mcmap.net/q/77186/-how-to-unpack-an-asar-fileForsta
S
41

The best tool I know is NodeJS tool: vercel/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.

Shantelleshantha answered 26/10, 2017 at 19:43 Comment(2)
I tried to compact an Appium installation into an Exe file and zeit/pkg failed completely. From the 310MB on disk resulted a small Exe file of 18MB which obviously did not work at all.Wendy
Repo name is now updated as vercel/pkgOrang
O
29

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.

Outpatient answered 17/11, 2011 at 23:16 Comment(5)
As you mentioned, this doesn't solve my issue in an ideal way. But +1 for the interesting readsDarnelldarner
@Aishwar, I've added a simple app that will launch Node for you. See edits.Outpatient
@Outpatient does your exe still require that you have node.exe in the same folder?Ninos
@RicardoTomasi, no. It assumes Node is installed in your system's Program Files directory (where the installer puts it).Outpatient
@Outpatient This is an interesting solution. I like what you have done, I wish I could +1 again :). But this is still not ideal, as it requires the user to have node.js installed and in the standard path.Darnelldarner
M
15

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/

Methadone answered 20/1, 2013 at 16:6 Comment(3)
Seems to be a broken link.Mammalogy
This project is no longer active. See node-webkit for an alternative as mentioned by Clayton Gulick.Scandal
Appjs itself suggests an active fork called deskshell github.com/sihorton/appjs-deskshellMethadone
D
15

Haven't tried it, but nexe looks like nexe can do this:

https://github.com/crcn/nexe

Duchess answered 1/9, 2014 at 19:48 Comment(0)
R
13

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)

Rightminded answered 18/12, 2014 at 16:24 Comment(4)
Thanks mate. Can I make a linux executable from Windows?Prowel
As far as I know, no, you can't. I think JXCore grabs some files from the OS to make the executable, and it doesn't package those files, so if you want to make a Linux executable, you'll have to run this command on Linux.Rightminded
JXcore has stopped development nubisa.com/nubisa-halting-active-development-on-jxcore-platformIndebted
All the links here are dead. This works: github.com/jxcore/jxcore-releaseWendy
S
11

I was using below technology:

  1. @vercel/ncc (this make sure we bundle all necessary dependency into single file)
  2. pkg (this to make exe file)

Let do below:

  1. npm i -g @vercel/ncc

  2. 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)

  3. npm install -g pkg (installing pkg)

  4. pkg index.js (make sure you are in the dist folder above)

Salmanazar answered 16/12, 2020 at 4:35 Comment(0)
D
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,

  1. No GUI. Pure CLI.
  2. No run-time dependencies
  3. Supports both Windows and Unix
  4. Runs 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.

Desired answered 4/8, 2016 at 10:6 Comment(2)
@StéphaneLaurent Check out node-compiler, it definitely supports using a command-line argument with the exe.Desired
Latest commit on Aug 30, 2017 (2 years ago). Discontinued?!Cida
U
5

nexe npm package creates a single executable out of your node.js apps

nexe git repo

Unskilled answered 1/8, 2017 at 16:34 Comment(0)
C
5

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)

Cassatt answered 31/12, 2021 at 13:16 Comment(0)
R
4

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

Recess answered 25/3, 2014 at 13:12 Comment(0)
M
4

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.

Moselle answered 9/1, 2017 at 15:14 Comment(1)
It also support good code protection because data saved inside RAM but not in Hard DriveScone
B
4

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.

Bonin answered 28/6, 2017 at 15:34 Comment(2)
Electron does not justify to be a valid suggestion for the OP's issue; it is an entire different Node framework.Jeanett
Great comparison!Conquer
J
3

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.

Jaehne answered 1/5, 2017 at 5:40 Comment(0)
H
2

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);
        }
Hasen answered 26/5, 2012 at 23:14 Comment(0)
H
2

Since release v18.16.0 it is possible to create single executable application using bundled source code

Height answered 11/12, 2023 at 12:27 Comment(0)
A
1

If you have Node.js 20 installed or 19.7.0, v18.16.0, you can create a .exe file natively using the experimental Single executable applications feature. It can also create binaries that can work on Mac and Linux. As mentioned, it is experimental, so it is subject to change.

Active answered 11/6, 2023 at 21:14 Comment(0)
T
0

There is an opensource build tool project called nodebob. I assume that working platform windows,

  1. simply clone or download project
  2. copy all node-webkit project files of yours into the app folder.
  3. run the build.bat script.

You will find the executable file inside the release folder.

Tew answered 21/4, 2017 at 17:26 Comment(0)
D
0

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.

Deirdre answered 2/11, 2021 at 9:18 Comment(1)
Update to my answer: I discovered something called nvm, You could use that instead of nodeenv. I don't know how, but you could. You could also use the official node.js installer instead of nodeenv and install it in the directory of your project. I don't know how, for this one too.Deirdre
H
0

One way to do this is to publish your code as an NPM module, and supply NPM with a bin file in the package.json with "bin": "app.js"1

For users to install your CLI, they will have to have node installed and run npm i -g [packageName] (-g is for global)

this answer is very simple but has the downside of requiring node to be installed.

If you want your app to actually be an executable without the help of node or NPM, i would recommend nexe

Heirship answered 5/8, 2022 at 13:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.