16

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
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/pkg Commented Jun 9, 2021 at 5:01
  • 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 code Commented Jun 9, 2021 at 5:56
  • @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? Commented Jun 9, 2021 at 8:54

2 Answers 2

34

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Add --targets node8-linux (or other versions) if your current node version is not supported by pkg.
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.