Is there a way to compile node.js source files?

node.jsCompilation

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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

Solution 2 - node.js

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

Solution 3 - node.js

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.

Solution 4 - node.js

There was an answer here: https://stackoverflow.com/questions/9413123/secure-distribution-of-nodejs-applications. Raynos said: V8 allows you to pre-compile JavaScript.

Solution 5 - node.js

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.

Solution 6 - node.js

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.

Solution 7 - node.js

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarkView Question on Stackoverflow
Solution 1 - node.jsMetal3dView Answer on Stackoverflow
Solution 2 - node.jsTor PView Answer on Stackoverflow
Solution 3 - node.jsRobin RodricksView Answer on Stackoverflow
Solution 4 - node.jsAndreView Answer on Stackoverflow
Solution 5 - node.jsRaynosView Answer on Stackoverflow
Solution 6 - node.js1j01View Answer on Stackoverflow
Solution 7 - node.jsAlfredView Answer on Stackoverflow