Home Web Front-end JS Tutorial Let's talk about how to use pkg to package Node.js projects into executable files.

Let's talk about how to use pkg to package Node.js projects into executable files.

Dec 02, 2022 pm 09:06 PM
node.js node

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package the Node project into an executable file. I hope it will be helpful to you!

Let's talk about how to use pkg to package Node.js projects into executable files.

Using pkg, you can package your Node.js project into an executable file that can even be run on devices where Node.js is not installed. [Related tutorial recommendations: nodejs video tutorial]

Experimental environment

  • Operating system: windows

  • node version: 16.14.2

##Operation process

  • Download PKG

We can choose to install it globally and execute it in any directory:

$ npm install -g pkg
Copy after login
  • Packaging program

Write a simple program first, such as server.js content

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello World!');
});

app.listen(3000, () => {
    console.log('Express web app on localhost:3000');
});
Copy after login
Enter the nodejs project root directory and execute the following command

$ pkg server.js
Copy after login

First time Error

At this time, an error will be reported

$ pkg server.js
> pkg@5.6.0
> Targets not specified. Assuming:
  node16-linux-x64, node16-macos-x64, node16-win-x64
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v16.14.2-linux-x64          [                    ] 0%> Not found in remote cache:
  {"tag":"v3.3","name":"node-v16.14.2-linux-x64"}
> Building base binary from source:
  built-v16.14.2-linux-x64
> Error! Not able to build for 'linux' here, only for 'win'
Copy after login
The main idea is that the current environment only supports executable files compiled into windows systems, that is, the win

adjustment instructions are:

$ pkg -t win server.js
Copy after login
Where -t win is equivalent to --targets win, which means that files are only compiled for windows.

Second error report

An error message was reported again during compilation:

$ pkg -t win server.js
> pkg@5.6.0
> Fetching base Node.js binaries to PKG_CACHE_PATH
  fetched-v16.14.2-win-x64            [                    ] 0%> Not found in remote cache:
  {"tag":"v3.3","name":"node-v16.14.2-win-x64"}
> Building base binary from source:
  built-v16.14.2-win-x64
> Fetching Node.js source archive from nodejs.org...
> Error! AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
Copy after login
The main idea is that the corresponding binary file fetched-v16.14.2-win is missing from the cache -x64, we just need to download the corresponding file and put it in the corresponding cache directory.

1. Go to the

official website to download the corresponding version file. For example, mine is node-v16.14.2-win-x64

Official website address: https://github.com /vercel/pkg-fetch/releases

Lets talk about how to use pkg to package Node.js projects into executable files.

2. Rename the file node-v16.14.2-win-x64 downloaded in the previous step to fetched-v16.14.2-win -x64, put it in the current user's cache directory.

For example, my cache directory is

C:\Users\MangoDowner.pkg-cache. After splicing the fetch tag, it becomes the final directory. Refer to the information in the error report to get The tag is v3.3

 {"tag":"v3.3","name":"node-v16.14.2-win-x64"}
Copy after login
We can get the final parent directory as C:\Users\MangoDowner.pkg-cache\v3.3,

So the final file address is C:\Users\MangoDowner .pkg-cache\v3.3\fetched-v16.14.2-win-x64

Compile again, success!

$ pkg -t win server.js
> pkg@5.6.0
Copy after login
For more node-related knowledge, please visit:

nodejs tutorial!

The above is the detailed content of Let's talk about how to use pkg to package Node.js projects into executable files.. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An article about memory control in Node An article about memory control in Node Apr 26, 2023 pm 05:37 PM

The Node service built based on non-blocking and event-driven has the advantage of low memory consumption and is very suitable for handling massive network requests. Under the premise of massive requests, issues related to "memory control" need to be considered. 1. V8’s garbage collection mechanism and memory limitations Js is controlled by the garbage collection machine

Detailed graphic explanation of the memory and GC of the Node V8 engine Detailed graphic explanation of the memory and GC of the Node V8 engine Mar 29, 2023 pm 06:02 PM

This article will give you an in-depth understanding of the memory and garbage collector (GC) of the NodeJS V8 engine. I hope it will be helpful to you!

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

Let's talk in depth about the File module in Node Let's talk in depth about the File module in Node Apr 24, 2023 pm 05:49 PM

The file module is an encapsulation of underlying file operations, such as file reading/writing/opening/closing/delete adding, etc. The biggest feature of the file module is that all methods provide two versions of **synchronous** and **asynchronous**, with Methods with the sync suffix are all synchronization methods, and those without are all heterogeneous methods.

An in-depth analysis of Node's process management tool 'pm2” An in-depth analysis of Node's process management tool 'pm2” Apr 03, 2023 pm 06:02 PM

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Let's talk about the event loop in Node Let's talk about the event loop in Node Apr 11, 2023 pm 07:08 PM

The event loop is a fundamental part of Node.js and enables asynchronous programming by ensuring that the main thread is not blocked. Understanding the event loop is crucial to building efficient applications. The following article will give you an in-depth understanding of the event loop in Node. I hope it will be helpful to you!

What should I do if node cannot use npm command? What should I do if node cannot use npm command? Feb 08, 2023 am 10:09 AM

The reason why node cannot use the npm command is because the environment variables are not configured correctly. The solution is: 1. Open "System Properties"; 2. Find "Environment Variables" -> "System Variables", and then edit the environment variables; 3. Find the location of nodejs folder; 4. Click "OK".

See all articles