single-package-executable-Nodejs

How to Integrate your Node.js application into a single executable?

Let’s discuss, How to Integrate your Node.js application into a single executable package?

Introduction

Are you facing many problems while running or testing your nodeJs project in different versions and different operating systems, where you have to install all npm packages every time? YES! Then pkg npm package helps you to create a single executable file (.exe file) to run your nodeJs project easily at any time, anywhere.

You can use this command-line interface to package your Node. js project into an executable that can be launched on devices that don’t have Node installed.

Hire nodejs developers india

Usage of Pkg npm:

  • Create a commercial version of your software without the source code.
  • Without installing the updated Node.js version, you can test your app.
  • Make a source-free demo/evaluation/trial version of your app.
  • Create executables for additional platforms in a matter of seconds (cross-compilation)
  • Create a self-extracting archive or installation of some sort.
  • To launch the packed application, you don’t need to install Node.js or npm.
  • To deploy your application, you don’t need to download hundreds of files using npm install. It should be deployed as a single file.
  • To make it even more portable, include your assets inside the executable.

Steps to create your project’s executable file:

Step: 1

Open your nodeJs project

Step: 2

Install pkg npm package globally using the below command.

npm install -g pkg

Step: 3

During the packaging process, pkg parses your sources, detects require calls, traverses your project’s dependencies, and includes them in the executable. In most cases, no manual configuration is required. However, your code may contain require(variable) calls (also known as a non-literal argument to require) or use non-javascript files (for example views, CSS, images, etc).

Configure app.js file

  • app.set(‘views’, path.join(__dirname, ‘views’));
  • Give path to Env
    • require(“dotenv”).config({ path: path.join(__dirname, ‘../.env’) });

Step: 4

pkg does not handle such cases. As a result, you must manually specify the files – scripts and assets – in your package.json.

"pkg": {
  "assets": [
    "views/**/*",
    "public/**/*",
    ".env"
  ]
}

Step: 6

Execute the pkg build for multiple platforms. The executable will be created in the dist directory.

Targets

pkg can create executables for multiple target machines at the same time. The –targets option allows you to specify a comma-separated list of targets. A canonical target is made up of three elements separated by dashes, such as node6-macOS-x64 or node4-Linux-armv6:

  • nodeRange node${n} or latest
  • platform FreeBSD, Linux, alpine, macOS, windows
  • arch x64, x86, armv6, armv7

You are free to leave out any component (and specify just node6 for example). The elements that are missing will be taken from the current platform or system-wide Node.js installation (its version and arch).

Create your executable file with the below command:

pkg. –targets node14-win

Output:

You can see the .exe file in your project folder:

npm packages

Step: 7

Run your .exe file to start your project.

Debug:

To obtain a log of the packaging process, use –debug with pkg. If you’re having trouble with a specific file (it doesn’t appear to be packaged as an executable), you might want to look through the log.

In conclusion:

Similarly, The main purpose to use the pkg npm package is that you can test your nodeJs project very quickly and share your project very easily without installing nodeJs and npm packages.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.