How to Compile Node.js Code Using Bytenode? (2025)

How to Compile Node.js Code Using Bytenode? (1)

In this post, I will show you how to “truly” compile Node.js (JavaScript) code to V8 Bytecode. This allows you to hide or protect your source code in a better way than obfuscation or other not-very-efficient tricks (like encrypting your code using a secret key, which will be embedded in your app binaries, that’s why I said “truly”above).

So, using bytenode tool, you can distribute a binary version.jsc of your JavaScript files. You can also bundle all your.js files using Browserify, then compile that single file into.jsc.

Check bytenode repository onGithub.

Long storyshort..

  1. Install bytenode globally:[sudo] npm install -gbytenode
  2. To compile your.js file, run this command:bytenode --compile my-file.js my-file.jsc
  3. Install bytenode in your project too:npm install --savebytenode
  4. In your code, require bytenode,const bytenode = require('bytenode');to register.jsc extension in Node.js module system. That’s why we installed it locallytoo.
  5. You can now require my-file.jsc as a module:const myFile = require('./my-file.jsc');You can also remove my-file.js from production build.
  6. And if you want to run my-file.jsc using bytenode cli:bytenode --run my-file.jsc

Now you know how to compile.js files, how to require the compiled version in your code, and how to run.jsc files from the terminal. Let’s move on to the longstory.

V8 engine (which Node.js is based on) uses what is called: just in time compilation (JIT), where JavaScript code is compiled just before execution, then it will be optimised subsequently.

Starting from Node.js v5.7.0, the vm module introduced a property called produceCachedData in vm.Script Constructor function, so if you do something likethis:

Then, get the bytecode or cachedData buffer:

This helloBuffer can be used to create an identical script that will execute the same instructions when it run, by passing it to the vm.Script Constructor function:

But this will fail, V8 engine will complain about the first argument (that empty string ''), when it checks whether it is the same code as the one that was used to generate helloBuffer buffer in the first place. However, this checking process is quite easy, it is the length of the code that does matter. So, this willwork:

We give it an empty string with the same length (28) as the original code (console.log("Hello World!");). That’sit!

This is interesting, using the cached buffer and the original code length we were able to create an identical script. Both scripts can be run using.runInThisContext(); function. So if you ranthem:

you will see ‘Hello World!’twice.

(Note that if you have used the wrong length, or if you have used another version of Node.js/V8: anotherHelloScript won’t run, and its property cachedDataRejected will be set totrue).

Now to our last step, when we defined anotherHelloScript we used a hard coded value (28) as our code length. How can we change this, so that in the runtime we don’t have to know exactly how long was the original sourcecode?

After some digging in V8 source code, I have found that the header information is defined here (in this file snapshot.h):

But, Node.js buffer is Uint8Array typed array. This means that each entry from the uint32 array will take four entries in the uint8 buffer. So, the payload length willbe:

It will be some thing like this: <Buffer 1c 00 00 00>, which is Little Endian, so it reads: 0x0000001c. That is our code length (28 in decimal).

To convert these four bytes to a numeric value, you may do something likethis:

firstByte + (secodeByte * 256) + (thirdByte * 256**2) + (forthByte *256**3),

Or in a more elegant way, you can dothis:

As I did here in my library, check it to see the fullrecipe.

Alternatively, we could use buf.readIntLE() function, which does exactly what wewant:

Once you have read the length of the original code (that was used to generate the cachedData buffer), you can now create yourscript:

Finally, does this technique have an impact on performance? Well, in recent versions of v8 (and Node.js), the performance is almost the same. Using octance benchmark I did not find any difference in performance. I know that Google deprecated octance (because browsers and JS engines were cheating), but the results in our situation are significant, because we are comparing the same code on the same JS engine. So, the final answer is: Bytenode does NOT have a negative impact on performance.

Check my Github repository, where you can find complete working examples. I have added an example for Electron (which has no source code protection at all) and for NW.js (which has a similar tool nwjc, but it works only with browser-side code). I will add more examples (and tests) soon, hopefully.

How to Compile Node.js Code Using Bytenode? (2)

How to Compile Node.js Code Using Bytenode? was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

How to Compile Node.js Code Using Bytenode? (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Velia Krajcik

Last Updated:

Views: 6053

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.