compile NodeJS to binary
Asked Answered
S

2

15

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
Superpower answered 9/6, 2021 at 3:55 Comment(3)
There isn't much context in the question so I am really not sure what is the end goal here. However one of the option would be to use pkg https://www.npmjs.com/package/pkgLingerie
1. PKG doesn't work for me, i don't have admin powers. 2. I am compiling this to prevent other people from stealing my codeSuperpower
@justanoob How do you have no controll over your own code? "Compile" the code on your developer machine and deploy the exectuable... You are telling us you dont have admin rights on your own computer?Chandrachandragupta
K
31

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.

Kingbolt answered 13/11, 2021 at 13:33 Comment(1)
Add --targets node8-linux (or other versions) if your current node version is not supported by pkg.Disadvantaged
T
3

Solver 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

Twice answered 30/3 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.