SoFunction
Updated on 2025-04-14

A detailed explanation of how to package Javascript into an exe executable file

What is pkg?

pkgis a command line tool that can package projects and their dependencies into a separate executable file. Users can run your program without installing the environment. Supported operating systems include:

  • Windows (generated.exe)

  • macOS (generate binary files)

  • Linux (generate binary files)

Install pkg

Install globally via npm or as a project dependency:

# Global installation (recommended)npm install -g pkg

# Or install as a project development dependencynpm install pkg --save-dev

Basic use

Step 1: Prepare your project

Suppose you have a simple script

// 
("Hello from pkg!");

Step 2: Package via the command line

Run the following command in the terminal:

pkg  --targets node18-win-x64,node18-macos-x64,node18-linux-x64 --output myapp
  • --targets: Specify the target platform and version (for examplenode18-win-x64represents 18 + Windows 64 bit).

  • --output: Output file name (the suffix will be automatically added according to the platform, such as.exe)。

Step 3: Run the generated executable file

Generated(Windows) ormyapp(macOS/Linux) can run directly without the need for an environment.

Configuration

It is more recommended inConfigure the pkg parameters:

{
  "name": "myapp",
  "version": "1.0.0",
  "scripts": {
    "build": "pkg . --targets node18-win-x64,node18-macos-x64,node18-linux-x64 --output myapp"
  },
  "pkg": {
    "assets": ["public/**/*", "views/**/*"],  // Contains static resources    "scripts": ["scripts/*.js"]  // Includes extra scripts  }
}

Runnpm run buildYou can pack it.

Processing resource files

If your project contains static files (such as images and HTML templates), please note:

  • Relative path issues:use__dirnameor()Make sure the path is correct.

  • Declare resources in

    {
      "pkg": {
        "assets": "public/**/*"
      }
    }

Advanced Usage

Specify version and platform

List of supported platforms:

pkg -h  # View all supported target combination

Pack the entire project

Pack directlyEntry file:

pkg .

Handle environment variables

Pass in the codeDetermine whether it is running in a packaged environment:

if () {
  ("Running in packaged mode!");
}

Frequently Asked Questions

Problem 1: Dynamic import module failed

  • reasonpkgUnable to process dynamicsrequire()(likerequire((__dirname, file)))。

  • solve:existPredeclares all possible dynamically loaded files.

Issue 2: File path error

  • reason: The file system path changes after packaging.

  • solve:use()Get the directory where the executable file is located.

Question 3: Missing dependencies

  • reason: Not heredependenciesDependency is declared in  .

  • solve: Make sure all dependencies are inofdependenciesmiddle.

Example: Packaging Express apps

  • Project structure:

    my-express-app/
    ├── 
    ├── public/
    │   └── 
    └── 
  • Code:

    const express = require('express');
    const path = require('path');
    const app = express();
    
    (((__dirname, 'public')));
    
    (3000, () => {
      ('Server running on port 3000');
    });
  • Configuration:

    json

    {
      "pkg": {
        "assets": "public/**/*"
      }
    }
  • Packaging command:

    pkg  --targets node18-win-x64 --output my-express-app

Summarize

  • usepkgIt is easy to convert a project into an executable file.

  • Pay attention to handling static resource paths and dynamic module loading.

  • passMore convenient configuration.

This is the article about packaging Javascript into an exe executable file. For more related contents of packaging Javascript into an executable file, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!