node.jsdeploymentbundleexecutabledistribute

Bundle and Deploy my nodejs program as an executable


I have a program written with node js that I want to bundle and distribute as "stand-alone" executable program.

I want to run the program through cmd only with the the executable file name (without using npm run start or node file.js). i.e. my_program arguments.

What is the most recommended way to achieve this?

Thanks.


Solution

  • There are several options you can choose from. I would recommend checking out Pkg.

    With Pkg, you can package your node application into a single executable for Windows, Linux or Mac.

    Simply install Pkg globally on your machine by running the command:

    npm install -g pkg
    

    and then add your point of entry to the package.json file as shown below:

    {
       "bin": "bin.js" // or whatever your point of entry is
    }
    

    Afterwards, from your app directory simply run the command

    pkg .
    

    This would build the executables for Windows, Linux and MacOS.

    You can execute the executable by running:

    Windows: your_exec-win.exe # windows

    Linux: chmod u+x your_exec-linux; ./your_exec-linux