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

2019-01-01 14:12发布

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.

17条回答
荒废的爱情
2楼-- · 2019-01-01 14:45

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.

查看更多
若你有天会懂
3楼-- · 2019-01-01 14:46

For anyone stumbling upon this question, there are now two projects that create exes out of your node projects, EncloseJS and Electron.atom.io , they differ slightly:

  1. EncloseJS will compile your project to native code, they also include assets AND a nodejs installation ( the system won't need to install nodejs to execute the .exe ). 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.
  2. Electron.atom.io while it will not compact and generate an .exe 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, mac osx, linux ), the cons are that your source code WILL be included, even tough 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.
查看更多
明月照影归
4楼-- · 2019-01-01 14:48

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);
        }
查看更多
无色无味的生活
5楼-- · 2019-01-01 14:50

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.

查看更多
君临天下
6楼-- · 2019-01-01 14:53

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/

查看更多
余欢
7楼-- · 2019-01-01 14:54

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)

查看更多
登录 后发表回答