How to package a Node.js project into an executable?

Asked

Viewed 4,446 times

25

I have a Node.js project but I need to create an executable for Windows and one for Linux, both of which still contain the built-in Node.js.

I managed to do something similar in an app node-webkit using a plugin for Grunt, but would like to know how to do this in a normal Node.js application.

  • 1

    Take a look at this link: http://stackoverflow.com/questions/8173232/make-exe-files-from-node-js-app. Apparently there are some tools for this job.

  • I think the best option would be to create a program using electron

2 answers

16

The solution is to use something like Appjs. It creates a package that contains your Nodejs application as well as Runtime (Node.exe), dependencies, and also provides the possibility to use a library that allows you to create Guis using HTML.

  • 1

    I believe that the bad thing about using Appjs is that it comes with the whole GUI, if one is not needed. It ends up weighing a lot!

  • Exactly, what I want is to create a command line app.

  • If I’m not mistaken, just don’t use app.createWindow(..) that it works the same as any other Nodejs application. Maybe it’s even possible not to even give require('appjs') making it possible to use the packaging part of Appjs. The only problem with this is that the Chromium library goes along with the packaged application no matter how much you don’t use it, which should increase the size of it, relative to if you didn’t have GUI.

  • I will test this way that you suggested and if it goes well I confirm your answer. Thank you very much.

11

Currently there doesn’t seem to be many options for distributing a binary standalone of a Node.js application.

One of them, and practically unique, option is the Nexe. Does just what you want, but there are some (big) problems:

  • It can be a bit unstable (your code is very simple).
  • Does not support native modules.
  • Does not support Windows.

Despite all the cons... let’s go to one hello world:

1. Installing the Nexe:

 $ sudo npm install -g nexe

2. Creating a test file:

$ echo "console.log('Hello World');" > hello.js

3. Compiling the application:

$ nexe -i hello.js -o hello
making application bundle with /home/talles/hello.js
writing application bundle:
[a partir daqui vêm uma saída 'pesada' do compilador]

-i, of input, are the input files; -o, of output, the output file. There is also -t temporary directory and -r of Runtime (version of the number to be used).

4. Ready, the final file is generated:

$ ./hello
Hello World

In the example the generated final file had a size of 11Mb.

  • 1

    Not so new, I looked at the commit history. There are few commits scattered across more than 2 years of the project’s existence.

  • @gustavohenke truth...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.