Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compile NodeJS to binary

i want to compile nodeJS into binary, similar to C/C++.
compile

$NodeCompile -ofast Node.js -o executable

(fine if i can't do compiler settings)

to run it

$./executable
like image 922
just a noob Avatar asked Dec 22 '25 19:12

just a noob


2 Answers

You probably want to look at pkg

It somehow creates a self-contained binary executable from Javascript, including module dependencies and asset files.

Installation and use is easy:

$ npm install -g pkg
$ pkg index.js -o my-program
$ ./my-program

My understanding is that this binary contains nodejs bytecode. It also appears that you can cross-compile.

Note: I've tried ncc and nexe also, but I haven't found them to be as useful. ncc just creates a self-contained Javascript file and nexe encountered a Python error when I tried to use it.

like image 187
Adonis Gaitatzis Avatar answered Dec 24 '25 09:12

Adonis Gaitatzis


Solved the same thing using Bun. I added an entry to script in package.json, like this:

"scripts": {
   "compile": "bun build src/main.ts --compile --outfile out"

I now run it like bun run compile and i can use the out file like ./out

like image 44
Tycho Avatar answered Dec 24 '25 09:12

Tycho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!