Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to precompile node.js scripts?

Is there a way to precompile node.js scripts and distribute the binary files instead of source files?

like image 740
Mark Avatar asked Aug 15 '12 07:08

Mark


People also ask

Can JavaScript be precompiled?

Pre-compiled JavaScript # The most popular are 0.10, 0.12, and the newer versions 4 and 5. We can pre-build a single bundle for each of these versions during the build step (on the dev or CI machine). During the installation by the client, we can determine which bundle to use depending on the Node version.

Is there a way to compile JavaScript?

Two of the most commonly used packages used to compile JavaScript files into executables are: nexe: It is a simple command-line utility that compiles your Node. js application into a single executable file. By default, it converts it into a Windows executable.

CAN node be compiled?

Standard Node. js is built against V8, which compiles every Javascript code snippet into native instructions. You may use --print_code flag in the command line to see which scripts are getting compiled, and compiled into what.


1 Answers

Node already does this.

By "this" I mean creating machine-executable binary code. It does this using the JIT pattern though. More on that after I cover what others Googling for this may be searching for...

OS-native binary executable... If by binary file instead of source, you mean a native-OS executable, yes. NW.JS and Electron both do a stellar job.

Use binaries in your node.js scripts... If by binary file instead of source, you mean the ability to compile part of your script into binary, so it's difficult or impossible to utilize, or you want something with machine-native speed, yes. They are called C/C++ Addons. You can distribute a binary (for your particular OS) and call it just like you would with any other var n = require("blah");

Node uses binaries "Just In Time"

Out of the box, Node pre-compiles your scripts on it's own and creates cached V8 machine code (think "executable" - it uses real machine code native to the CPU Node is running on) it then executes with each event it processes.

Here is a Google reference explaining that the V8 engine actually compiles to real machine code, and not a VM.

Google V8 JavaScript Engine Design

This compiling takes place when your application is first loaded.

It caches these bits of code as "modules" as soon as you invoke a "require('module')" instruction.

It does not wait for your entire application to be processed, but pre-compiles each module as each "require" is encountered.

Everything inside the require is compiled and introduced into memory, including it's variables and active state. Again, contrary to many popular blog articles, this is executed as individual machine-code processes. There is no VM, and nothing is interpreted. The JavaScript source is essentially compiled into an executable in memory.

This is why each module can just reference the same require and not create a bunch of overhead; it's just referencing a pre-compiled and existing object in memory, not "re-requiring" the entire module.

You can force it to recompile any module at any time. It's lesser-known that you actually have control of re-compiling these objects very easily, enabling you to "hot-reload" pieces of your application without reloading the entire thing.

A great use-case for this is creating self-modifying code, i.e. a strategy pattern that loads strategies from folders, for example, and as soon as a new folder is added, your own code can re-compile the folders into an in-line strategy pattern, create a "strategyRouter.js" file, and then invalidate the Node cache for your router which forces Node to recompile only that module, which is then utilized on future client requests.

The end result: Node can hot-reload routes or strategies as soon as you drop a new file or folder into your application. No need to restart your app, no need to separate stateless and stateful operations: Just write responses as regular Node modules and have them recompile when they change.

Note: Before people tell me self-modifying code is as bad or worse than eval, terrible for debugging and impossible to maintain, please note that Node itself does this, and so do many popular Node frameworks. I am not explaining original research, I am explaining the abilities of Google's V8 Engine (and hence Node) by design, as this question asks us to do. Please don't shoot people who R the FM, or people will stop R'ing it and keep to themselves.

"Unix was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things." – Doug Gwyn

Angular 2, Meteor, the new opensource Node-based Light table IDE and a bunch of other frameworks are headed in this direction in order to further remove the developer from the code and bring them closer to the application.

How do I recompile (hot-reload) a required Node module?

It's actually really easy... Here is a hot-reloading npm, for alternatives just Google "node require hot-reload"

https://www.npmjs.com/package/hot-reload

What if I want to build my own framework and hot-reload in an amazing new way?

That, like many things in Node, is surprisingly easy too. Node is like jQuery for servers! ;D

stackoverflow - invalidating Node's require cache

like image 51
Nick Steele Avatar answered Oct 20 '22 14:10

Nick Steele