Obfuscating NodeJS Code

Obfuscating NodeJS Code

Obfuscating node.js code as a binary

Step by step guide to set up pkg to compile your node.js code into a binary file.

  1. Install pkg (this guide assumes version 4.3.8):

    npm i -g pkg
    
  2. Create a pkg.json config file. This will point pkg to any assets that need to be bundled with the code. For example, if there is a html frontend whose static files are hosted and read from a client folder in the top level of its repo, the resulting config would look like this:

    {
        "assets": [ "dist/client/**/*", "package.json" ]
    }
    
  3. Compile the code. Point it to the root of the node code and the config file we just created. E.G.:

    pkg ./dist/server/index.js -c pkg.json
    

    or into package.json like:

    "compile": "npm run preversion && pkg ./dist/server/index.js -c pkg.json --targets linux-armv7 --output pkg/robot2cnc"
    

    ** NOTE: Crosscompilation between processors is not possible. Therefore you cannot compile on an x64 architecture (Mac or Windows) and run on armv7 (Raspberry Pi). A suggested solution is here. **

  4. This will create compiled, binary versions of the project.

    If --targets is not specified, it will create one for each OS: linux, Windows, and OSX

    • index-linux
    • index-macos
    • index-win.exe

    Find out more about targets in the documentation

  5. To use with Nitro or Robot2CNC, the Service will need to be edited to run the pkg

    To edit a service

     sudo systemctl edit --full vbxc.service
    
     OR
    
     sudo systemctl edit --full robot2cnc.service
    

    Then change the ExecStart setting to one of the following

     ExecStart=/home/vbxc/nitro/index-linux
    
     OR
    
     ExecStart=/home/pi/millipede/robot2cnc