Is there a way to compile node.js source files? [duplicate]
Asked Answered
R

7

87

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

Ruddy answered 26/5, 2011 at 21:52 Comment(3)
This appears to be part of a Duplicate Pool: #6146061, #7557864, https://mcmap.net/q/77051/-how-to-make-exe-files-from-a-node-js-app, #8794640, #9725317, #13388608Triquetrous
A good list of tools is here: https://mcmap.net/q/77054/-packaging-a-node-js-web-application-as-a-normal-desktop-application-closedHippocrates
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.Corliss
V
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

Valentinevalentino answered 14/2, 2013 at 22:38 Comment(10)
Just a note: This is only for Linux / Mac, and not windows.Lareelareena
@Valentinevalentino That's a pretty novel way of doing it; thanks.Crandale
@Metal3d, wow! I wish it supports Windows!!!Halsy
It is now supported on Windows, however it has a dependency on python which most Windows users don't have installed by default.Flyte
@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...Valentinevalentino
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.Flemming
EncloseJS is a new project that compiles a node .js file to a binary for windows/linux - enclosejs.comUndergraduate
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.Homochromous
Metal3d is the executable that is generated is cross-platform?Shamefaced
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." npmjs.com/package/nexeValentinevalentino
A
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

Anurag answered 18/2, 2012 at 12:42 Comment(8)
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?Visage
This is probably the best and most standard answer if combined with saving the compile instead of 'no reason'.Crandale
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.Predilection
@Visage Turning something into a binary doesn't keep people from reverse-engineering it. It's not hard.Scuttlebutt
@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.Visage
@Visage 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.Scuttlebutt
@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 :)Visage
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.Baldridge
U
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.
Undergraduate answered 16/3, 2015 at 18:8 Comment(5)
Free for non-commercial use. But really nice yeahValentinevalentino
It's closed source. The Russian botnet welcomes your app.Claque
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).Compensate
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!Appledorf
Yes it is free for non-commercial, but not trusted, I did not understand the term ' limited connections' ...Homochromous
O
10

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

Oxidate answered 6/9, 2012 at 7:59 Comment(0)
L
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.

Lalitta answered 26/5, 2011 at 21:54 Comment(7)
The OP probably wants to take a node.js application and compile it to native code.Ponder
I imagine it's closure that's wanted. Or more likely, jslint.Yvoneyvonne
@drachenstern Devil. Recommend jshint. jslint is too opinionated.Lalitta
~ 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.Yvoneyvonne
@raynos Is Closure compiler the one you would recommend to compile node.js code for obfuscation pruposes ?Dolores
@Dolores Closure compiler is good, it's the one I use. There are others like YUI compressor and uglify. There shouldn't be much differenceLalitta
Javascript is actually compiled into machine code in your browser. Your answer is invalid.Regurgitate
S
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.

Syndicalism answered 15/3, 2015 at 18:8 Comment(0)
B
-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.

Bonaparte answered 26/5, 2011 at 23:29 Comment(4)
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.Yvoneyvonne
@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 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??Bonaparte
Yes actually I was looking for an alternative solution to the addons, thanks thoughRuddy
@Marco your welcome :P. Hope you succeededBonaparte

© 2022 - 2024 — McMap. All rights reserved.