72

I am considering developing a desktop application composed of 2 parts:

  • user interface (Java app for example)
  • back-end Node.js server

The 2 parts connect through sockets. Don't ask why I know it's weird.

I will want to be able to provide to customers the application with an installer. I don't want that users have to install Node.js themselves.

Is there a way to have a Node.js server installed as standalone, i.e. no need to install Node.js globally on the system.

This is a question for any (Windows, Linux, Mac OS X...) environment.

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • 2
    A relevant question: http://stackoverflow.com/questions/8794140/is-it-possible-to-create-desktop-applications-with-node-js – Anderson Green Nov 15 '12 at 06:33
  • 4
    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:51
  • possible duplicate of [How do I deploy Node.js applications as a single executable file?](http://stackoverflow.com/questions/14314038/how-do-i-deploy-node-js-applications-as-a-single-executable-file) – Artjom B. Jul 31 '15 at 17:41
  • @ArtjomB. The question you linked to is 1 year older than mine. And [this question](http://stackoverflow.com/questions/7557364/packing-node-js-scripts-node-exe-into-a-single-executable) is specifically for Windows, which isn't answering my question. – Matthieu Napoli Aug 03 '15 at 11:00

7 Answers7

39

Update 2017-05-04: And there's a new kid in town:

Update 2016-11-14: Nowadays Electron and nwjs seem like the best options.

Original:

There are a number of steps you have to go through to create an installer and it varies for each Operating System. For Example:

coolaj86
  • 74,004
  • 20
  • 105
  • 125
18

You can bundle the binaries with your application. Won't have to install anything to run a Node app. The binaries are available on the same page as the installers.

You'll just have to know where the binaries are, but I assume you've got an installer that can put them somewhere known.

// To start the node process
$ /path/to/binaries/npm install
$ /path/to/binaries/node myApp.js
BadCanyon
  • 3,005
  • 19
  • 17
  • 1
    Is such packaging legal according to nodejs license? – Pacerier Oct 30 '17 at 21:42
  • 3
    it doesn't answer the question – Vyachaslav Gerchicov Jun 11 '18 at 14:56
  • This answer is outdated/incomplete, there is now a bunch of tools to handle this (see [@CoolAJ86's answer](https://stackoverflow.com/a/18703421/1439688)). Just tried [`pkg`](https://www.npmjs.com/package/pkg) on macOS, it generates one standalone executable file per platform, I tested the `.exe` on an out-of-the-box windows VM, works flawlessly. – zakinster Jan 03 '19 at 14:08
9

Node-Webkit is an option, but it really isn't set-up to do a "server - client" type relationship.

Another option is packaging the node.js installers with you application installer. Then when the application boot you can spin up a node.js process. I know some developers have been doing this with titanium, here is a little bit more information information.

Hope this helps!

Sdedelbrock
  • 5,192
  • 1
  • 18
  • 13
  • 2
    You can launch a server from node-webkit just in the way you launch it in Node.js. It's just that node-webkit provide another way beyond B/S architecture. – Roger Wang Nov 15 '12 at 08:17
  • This doesn't directly answer my question (so I've accepted another one) but this is awesome this is an even better solution that what I was planning to do!! Thank you!! – Matthieu Napoli Nov 15 '12 at 09:00
  • @RogerWang Could you explain how to launch a server from node-webkit – Premalatha Jun 25 '19 at 10:58
4

Here's an option: Light Table is a node app, but installs nicely and integrates the GUI (webkit) cleanly on most OSs.

To do this it leverages node-webkit. (Runs node code straight from an html page.) Here is the packaging documentation.

brandonscript
  • 68,675
  • 32
  • 163
  • 220
rdrey
  • 9,379
  • 4
  • 40
  • 52
3

Worth mentioning Electron made by GitHub. Used for building Atom, Slack, Visual Studio Code and more.

itamarb
  • 2,559
  • 3
  • 29
  • 35
1

I’ve just stumbled upon nexe – a tool which “creates a single executable out of your node.js app”.

I haven’t tried it out yet, but I guess that even works without an installer – producing just a single standalone binary.

tomekwi
  • 2,048
  • 2
  • 21
  • 27
  • 2
    Just tried using it. Had a lot of problems. It would fail silently a lot. I never got it working in some of my use cases so I switched to pkg mentioned above and had to problems. – Sobachatina Jan 24 '18 at 23:39
1

As of node.js 18.16.0 you can now generate single executable applications

Users can create a single executable application from their bundled script with the node binary itself and any tool which can inject resources into the binary.

Here are the steps for creating a single executable application using one such tool, postject:

  1. Create a JavaScript file:
echo 'console.log(`Hello, ${process.argv[2]}!`);' > hello.js
  1. Create a configuration file building a blob that can be injected into the single executable application (see Generating single executable preparation blobs for details):
echo '{ "main": "hello.js", "output": "sea-prep.blob" }' > sea-config.json
  1. Generate the blob to be injected:
node --experimental-sea-config sea-config.json
  1. Create a copy of the node executable and name it according to your needs:
  • On systems other than Windows:

    cp $(command -v node) hello
    
  • On Windows:

    Using PowerShell:

    cp (Get-Command node).Source hello.exe
    

    Using Command Prompt:

    for /F "tokens=*" %n IN ('where.exe node') DO @(copy "%n" hello.exe)
    

    The .exe extension is necessary.

  1. Remove the signature of the binary (macOS and Windows only):
  • On macOS:
codesign --remove-signature hello
  • On Windows (optional):

signtool can be used from the installed Windows SDK. If this step is skipped, ignore any signature-related warning from postject.

signtool remove /s hello.exe
  1. Inject the blob into the copied binary by running postject with the following options:
  • hello / hello.exe - The name of the copy of the node executable created in step 4.
  • NODE_SEA_BLOB - The name of the resource / note / section in the binary where the contents of the blob will be stored.
  • sea-prep.blob - The name of the blob created in step 1.
  • --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 - The fuse used by the Node.js project to detect if a file has been injected.
  • --macho-segment-name NODE_SEA (only needed on macOS) - The name of the segment in the binary where the contents of the blob will be stored.

To summarize, here is the required command for each platform:

  • On Linux:
npx postject hello NODE_SEA_BLOB sea-prep.blob \
    --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
  • On Windows - PowerShell:
npx postject hello.exe NODE_SEA_BLOB sea-prep.blob `
    --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2
  • On Windows - Command Prompt:
npx postject hello.exe NODE_SEA_BLOB sea-prep.blob ^
    --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 
  • On macOS:
npx postject hello NODE_SEA_BLOB sea-prep.blob \
    --sentinel-fuse NODE_SEA_FUSE_fce680ab2cc467b6e072b8b5df1996b2 \
    --macho-segment-name NODE_SEA
  1. Sign the binary (macOS and Windows only):
  • On macOS:
codesign --sign - hello
  • On Windows (optional):

A certificate needs to be present for this to work. However, the unsigned binary would still be runnable.

signtool sign /fd SHA256 hello.exe
  1. Run the binary:
  • On systems other than Windows
$ ./hello world
Hello, world!
  • On Windows
$ .\hello.exe world
Hello, world! 
bobbyg603
  • 3,536
  • 2
  • 19
  • 30