86

Is there a way to compile a node.js application?

Mark
  • 67,098
  • 47
  • 117
  • 162
  • 5
    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:48
  • 2
    A good list of tools is here: http://stackoverflow.com/a/12486874/32679 – GrGr Jun 30 '13 at 09:13
  • Deno has this built in, so it's probably a good move to use Deno instead of node.js when starting a project from scratch. – Martin Braun Jun 12 '21 at 01:14

7 Answers7

75

I maybe very late but you can use "nexe" module that compile nodejs + your script in one executable: https://github.com/crcn/nexe

EDIT 2021: Nexe's latest release is from 2017 and it appears that development has otherwise slowed, so the more-widely-used alternative from Vercel should also be considered these days: pkg

forresthopkinsa
  • 1,339
  • 1
  • 24
  • 29
Metal3d
  • 2,905
  • 1
  • 23
  • 29
  • 8
    Just a note: This is only for Linux / Mac, and not windows. – starbeamrainbowlabs Aug 09 '13 at 15:55
  • @Metal3d That's a pretty novel way of doing it; thanks. – That Realty Programmer Guy Dec 08 '13 at 23:55
  • 1
    @Metal3d, wow! I wish it supports Windows!!! – Edwin Yip May 03 '14 at 10:52
  • 6
    It is now supported on Windows, however it has a dependency on python which most Windows users don't have installed by default. – Henrik Karlsson May 14 '14 at 09:06
  • @Ineentho it seems that nexe can now compile windows binaries if I trust the requirements section "Windows: Python 2.6 or 2.7 (in PATH), Visual Studio 2010 or 2012" EDIT: sorry, I have not correctly read your answer, excuse my bad english... – Metal3d Aug 18 '14 at 02:49
  • 6
    Only the compiling Windows machine requires Python and Visual Studio. The client machines (those running the "compiled" Node.js script) do not have those dependencies. – Haz Jan 21 '15 at 17:48
  • EncloseJS is a new project that compiles a node .js file to a binary for windows/linux - http://enclosejs.com/ – Robin Rodricks Mar 16 '15 at 18:04
  • EncloseJS works fine but it is not free, nexe did not work sometimes the app terminate without any log or any exception, it is not stable. – Firas Abd Alrahman Feb 04 '17 at 15:39
  • Metal3d is the executable that is generated is cross-platform? – Akhil Surapuram Mar 11 '19 at 05:15
  • Hi, From npm repository documentation : "Its important to note: unless your native module conditionally loads each platform binary. Nexe builds with native modules will be platform specific. Eg. You will no longer be able to use cross platform builds." https://www.npmjs.com/package/nexe – Metal3d Mar 26 '19 at 15:41
18

Node.js runs on top of the V8 Javascript engine, which itself optimizes performance by compiling javascript code into native code... so no reason really for compiling then, is there?

https://developers.google.com/v8/design#mach_code

edhedges
  • 2,722
  • 2
  • 28
  • 61
Tor P
  • 1,351
  • 11
  • 9
  • 45
    By compiling your JavaScript source code, you will get some form of Binary or Byte Code (for example) in return, which is good if you don't want to reveal your source code. Does that make sense? – Mahdi Sep 19 '13 at 17:22
  • This is probably the best and most standard answer if combined with saving the compile instead of 'no reason'. – That Realty Programmer Guy Dec 08 '13 at 23:54
  • Also a daemon runs over a long period of time, giving the interpreter like v8 time to run multiple phases of optimization of the machine code. – Asad Hasan Apr 28 '14 at 20:32
  • 3
    @Mahdi Turning something into a binary doesn't keep people from reverse-engineering it. It's not hard. – Sven Slootweg Feb 12 '15 at 09:01
  • 3
    @SvenSlootweg of course, but you better hire a developer to write the same code, rather hire a hacker to reverse engineer tones of cpu instruction sets to a reasonable javascript source code. – Mahdi Feb 12 '15 at 09:19
  • 2
    @Mahdi The protections against somebody using your code without permission are primarily of a legal nature, not of a technical nature. It's unlikely that you'll gain anything from obfuscation-through-compilation in practice - on the other hand, it will almost certainly inconvenience 'legitimate' users. In other words: don't do it. – Sven Slootweg Feb 12 '15 at 10:20
  • 1
    @SvenSlootweg You're right to some degree, but I won't call it _obfuscation-through-compilation_. It is transforming the source code to an executable binary and it's incomparable to what we usually take as _Obfuscation_. But thanks anyways for the feedback :) – Mahdi Feb 12 '15 at 10:41
  • 54
    There's better reasons to compile code than obfuscation. If you want to distribute your program it's easier to give someone a binary than to tell them to install node. – stevendesu Mar 05 '15 at 01:07
15

EncloseJS.

You get a fully functional binary without sources.

Native modules also supported. (must be placed in the same folder)

JavaScript code is transformed into native code at compile-time using V8 internal compiler. Hence, your sources are not required to execute the binary, and they are not packaged.

Perfectly optimized native code can be generated only at run-time based on the client's machine. Without that info EncloseJS can generate only "unoptimized" code. It runs about 2x slower than NodeJS.

Also, node.js runtime code is put inside the executable (along with your code) to support node API for your application at run-time.

Use cases:

  • Make a commercial version of your application without sources.
  • Make a demo/evaluation/trial version of your app without sources.
  • Make some kind of self-extracting archive or installer.
  • Make a closed source GUI application using node-thrust.
  • No need to install node and npm to deploy the compiled application.
  • No need to download hundreds of files via npm install to deploy your application. Deploy it as a single independent file.
  • Put your assets inside the executable to make it even more portable. Test your app against new node version without installing it.
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • Free for non-commercial use. But really nice yeah – Metal3d Mar 17 '15 at 14:14
  • 8
    It's closed source. The Russian botnet welcomes your app. – papercowboy Apr 01 '15 at 07:24
  • 4
    This is a good idea, but I ran a few computation benchmarks and unfortunately the code with Enclose is about 5 times slower than with Node, so this IMHO beats the purpose unless the performance improves. Ran with -x flag on Win8.1 and Node 5 (64 bit exe). – grizzly Nov 20 '15 at 01:55
  • 3
    If you don't have a license (which is 9$/month) your program will have a nasty ouput message added saying to buy a license! – alexandre1985 Oct 19 '16 at 06:46
  • Yes it is free for non-commercial, but not trusted, I did not understand the term ' limited connections' ... – Firas Abd Alrahman Feb 04 '17 at 15:41
10

There was an answer here: Secure distribution of NodeJS applications. Raynos said: V8 allows you to pre-compile JavaScript.

Community
  • 1
  • 1
Andre
  • 139
  • 1
  • 6
5

You can use the Closure compiler to compile your javascript.

You can also use CoffeeScript to compile your coffeescript to javascript.

What do you want to achieve with compiling?

The task of compiling arbitrary non-blocking JavaScript down to say, C sounds very daunting.

There really isn't that much speed to be gained by compiling to C or ASM. If you want speed gain offload computation to a C program through a sub process.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 11
    The OP probably wants to take a node.js application and compile it to native code. – onteria_ May 26 '11 at 21:57
  • I imagine it's closure that's wanted. Or more likely, jslint. – jcolebrand May 26 '11 at 23:20
  • @drachenstern Devil. Recommend [jshint](http://jshint.com/). jslint is too opinionated. – Raynos May 26 '11 at 23:21
  • ~ You'll notice I didn't recommend it, I only indicated what the OP was likely asking for. Most devs think that the code validator is the compiler. They want to know that the code is valid, not that it runs without bugs (that's a potential bonus side effect). I would be happy if the cat remembered the rules of where a statement ended and decided to use lots of semicolons (more is better on newer devs) and more comments. – jcolebrand May 26 '11 at 23:35
  • @raynos Is Closure compiler the one you would recommend to compile node.js code for obfuscation pruposes ? – Luc Feb 02 '12 at 13:52
  • @Luc Closure compiler is good, it's the one I use. There are others like YUI compressor and uglify. There shouldn't be much difference – Raynos Feb 02 '12 at 14:31
  • Javascript is actually compiled into machine code *in your browser*. Your answer is invalid. – corazza Aug 06 '12 at 11:10
0

Now this may include more than you need (and may not even work for command line applications in a non-graphical environment, I don't know), but there is nw.js. It's Blink (i.e. Chromium/Webkit) + io.js (i.e. Node.js).

You can use node-webkit-builder to build native executable binaries for Linux, OS X and Windows.

If you want a GUI, that's a huge plus. You can build one with web technologies. If you don't, specify "node-main" in the package.json (and probably "window": {"show": false} although maybe it works to just have a node-main and not a main)

I haven't tried to use it in exactly this way, just throwing it out there as a possibility. I can say it's certainly not an ideal solution for non-graphical Node.js applications.

1j01
  • 3,714
  • 2
  • 29
  • 30
-6

javascript does not not have a compiler like for example Java/C(You can compare it more to languages like PHP for example). If you want to write compiled code you should read the section about addons and learn C. Although this is rather complex and I don't think you need to do this but instead just write javascript.

Alfred
  • 60,935
  • 33
  • 147
  • 186
  • 15
    that's not 100% valid. There are tools to turn javascript into bytecode. There are also tools to "compile" a code into closure, which is usually a) more compact, b) obeys the concept of scope, and c) is optimized for execution (compiler devs are usually multiply geniuses) so ... there are compilers, just not the same as DevEnv.exe for VS. – jcolebrand May 26 '11 at 23:36
  • 4
    @drachtenstern javascript in bytecode that can be used by node.js? Most of the times a compiler compiles a source from one language into another language often having a binary form(from http://en.wikipedia.org/wiki/Compiler). Those tools you are talking about still give you back javascript(only knew these kinds of tools) in an optimized form but still javascript. I am wondering if you can really call that a compiler?? – Alfred May 27 '11 at 00:03
  • Yes actually I was looking for an alternative solution to the addons, thanks though – Mark May 29 '11 at 01:53
  • 1
    @Marco your welcome :P. Hope you succeeded – Alfred May 29 '11 at 22:19