172

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.

peterh
  • 11,875
  • 18
  • 85
  • 108
Aishwar
  • 9,284
  • 10
  • 59
  • 80
  • 1
    Possible dupe: http://stackoverflow.com/questions/6145561/is-there-a-way-to-compile-node-js-source-files – Mike Christensen Nov 17 '11 at 23:25
  • 3
    This appears to be part of a Duplicate Pool: http://stackoverflow.com/questions/6145561/is-there-a-way-to-compile-node-js-source-files, http://stackoverflow.com/questions/7557364/packing-node-js-scripts-node-exe-into-a-single-executable, http://stackoverflow.com/questions/8173232/make-exe-from-node-js-app, http://stackoverflow.com/questions/8794140/is-it-possible-to-create-desktop-applications-with-node-js, http://stackoverflow.com/questions/9724817/how-to-create-a-stand-alone-command-line-application-with-node-js, http://stackoverflow.com/questions/13388108/standalone-node-js-application – Mogsdad Mar 10 '13 at 12:50
  • A good list of tools is here: http://stackoverflow.com/a/12486874/32679 – GrGr Jun 30 '13 at 09:12
  • 2
    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. – Liam Oct 31 '18 at 15:30
  • @Liam A good description of SO :) – john k Nov 21 '22 at 17:17

21 Answers21

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

Clayton Gulick
  • 9,755
  • 2
  • 36
  • 26
  • 2
    Just tried it, worked great! And it's supported by people at Intel. – Timo Huovinen Apr 12 '14 at 12:01
  • This should be the correct and most current answer as of today. See also the node webkit cheatsheet here: https://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"). – Erik Reppen Apr 24 '15 at 16:08
  • I like this answer, however this still does not create a standalone exe. According to [Intel](https://software.intel.com/en-us/html5/hub/blogs/deploying-hybrid-html5-games-on-the-desktop-using-node-webkit) and [nw.js at Github](https://github.com/nwjs/nw.js/wiki/How-to-package-and-distribute-your-apps) 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? – Jeff Baker May 05 '16 at 18:38
  • @JeffBaker if you look down a bit in the docs on github, you can see this: https://github.com/nwjs/nw.js/wiki/How-to-package-and-distribute-your-apps#an-alternative-way-to-make-an-executable-file-in-windows which you can do if you don't want to have to include additional files. – Clayton Gulick May 05 '16 at 21:28
  • @Clayton, Nw.js is bouched by chrome.com as well. https://developer.chrome.com/apps/migration#native – Pacerier Jan 30 '17 at 18:19
  • 4
    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. – Simon East Oct 01 '19 at 05:13
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.

MastAvalons
  • 1,150
  • 1
  • 14
  • 23
Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • 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. – Aishwar Nov 19 '11 at 20:54
  • 1
    @Aishwar, but you can't bundle node_modules folder, so you can't do anything advanced in it... – Clint Dec 13 '12 at 22:37
  • 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. – Jarrod Mosen Nov 30 '14 at 21:57
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.
KetZoomer
  • 2,701
  • 3
  • 15
  • 43
sokie
  • 1,966
  • 22
  • 37
39

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.

user8376713
  • 397
  • 3
  • 5
  • 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. – Elmue May 18 '18 at 17:30
  • 2
    Repo name is now updated as [vercel/pkg](https://github.com/vercel/pkg) – ikhvjs Oct 24 '22 at 09:52
28

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.

Community
  • 1
  • 1
josh3736
  • 139,160
  • 33
  • 216
  • 263
  • As you mentioned, this doesn't solve my issue in an ideal way. But +1 for the interesting reads – Aishwar Nov 17 '11 at 23:31
  • @Aishwar, I've added a simple app that will launch Node for you. See edits. – josh3736 Nov 18 '11 at 04:39
  • @josh3736 does your exe still require that you have node.exe in the same folder? – Ricardo Tomasi Nov 18 '11 at 09:54
  • @RicardoTomasi, no. It assumes Node is installed in your system's Program Files directory (where the installer puts it). – josh3736 Nov 18 '11 at 16:10
  • @josh3736 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. – Aishwar Nov 19 '11 at 01:04
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/

bitfed
  • 347
  • 4
  • 11
14

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

https://github.com/crcn/nexe

B T
  • 57,525
  • 34
  • 189
  • 207
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)

Nepoxx
  • 4,849
  • 5
  • 42
  • 61
  • 2
    Thanks mate. Can I make a linux executable from Windows? – Sasivarnan Feb 23 '15 at 14:44
  • 2
    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. – Nepoxx Feb 23 '15 at 14:54
  • 3
    JXcore has stopped development http://www.nubisa.com/nubisa-halting-active-development-on-jxcore-platform/ – user619271 May 25 '16 at 19:23
  • 2
    All the links here are dead. This works: https://github.com/jxcore/jxcore-release – Elmue May 18 '18 at 17:09
10

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)

Haryono
  • 2,184
  • 1
  • 21
  • 14
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.

Minqi Pan
  • 2,722
  • 2
  • 22
  • 26
5

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

nexe git repo

Dhrubo
  • 705
  • 1
  • 14
  • 33
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)

cibercitizen1
  • 20,944
  • 16
  • 72
  • 95
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

areve
  • 518
  • 4
  • 4
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.

Aminadav Glickshtein
  • 23,232
  • 12
  • 77
  • 117
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.

Community
  • 1
  • 1
Ali
  • 21,572
  • 15
  • 83
  • 95
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.

Rohit Luthra
  • 1,256
  • 17
  • 27
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);
        }
Wilk
  • 7,873
  • 9
  • 46
  • 70
origin1tech
  • 749
  • 6
  • 16
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.

cagcak
  • 3,894
  • 2
  • 21
  • 22
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.

  • 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. – Shaurya Chhabra Nov 05 '21 at 12:40
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

mavdotjs
  • 65
  • 6
0

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.

Stanley Ulili
  • 702
  • 5
  • 7