What is pkg?
pkg
is 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-x64
represents 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) or
myapp
(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 build
You can pack it.
Processing resource files
If your project contains static files (such as images and HTML templates), please note:
Relative path issues:use
__dirname
or()
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
reason:
pkg
Unable to process dynamicsrequire()
(likerequire((__dirname, file))
)。solve:exist
Predeclares 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 here
dependencies
Dependency is declared in .solve: Make sure all dependencies are in
of
dependencies
middle.
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
use
pkg
It is easy to convert a project into an executable file.Pay attention to handling static resource paths and dynamic module loading.
pass
More 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!