Package node service of pkg

       How to easily deploy services built with Node without an external network? Through self-search and practice, I found a suitable technology - pkg, which can package the service into an exe file, which is convenient for deployment. It can not only ensure the security of the code, but also allow users to operate with one click.

1. By searching for information, the technologies that come into contact with are:

     1. pm2: a process management, easy to learn, but this does not meet the results I want

     2. pkg: Good guy, isn't this what I want! ! pkg is a tool for compiling binary executable files, you can search and learn in npm: pkg - npm

     3. Others: nexe, node-packer, enclose, these are just a brief look

Two, pkg principle:

        In fact, pkg does not directly convert the program into an executable file, but packages the node environment into the program, so that it is not necessary to run the node project on a computer without a node environment. If you need to know more details, you can see this case node project is packaged into an executable file pkg (1): Introduction to pkg - Nuggets

3. Advantages:

        1. Output an exe file: it can be run on freebsd, linux, alpine, macos, win, depending on the usage and packaging. Note: When packaging, check your packaging environment. For example, on win, you can only package win executable packages

        2. Out of the node environment, there is no need to install a bunch of dependencies

        3. Separate from the external network, no need to connect to the Internet (disconnected) to use

        4. To prevent code leakage, only an exe file is given to the customer, and there is no need to publish all the code

        This is the reason why I chose pkg. Ok, now let’s talk about the implementation steps and the pitfalls I encountered

4. Use steps

1. Install pkg globally

npm install -g pkg

2. Configuration

Configure under package.json

(1) Entry file

     The principle of pkg to perform packaging is to start from the entry file, and follow the vines to execute the packaging process according to the relevant dependencies. Obviously, the entry file is the same as the node application itself. In the configuration file package.json, you need to add the bin attribute to specifically specify the execution entry of pkg. There are many ways to enter:

// package.json
{
...
"bin": "./bin/www",
"bin": "./service.js"
...
}

(2) Packaging command

Pkg execution is the most critical packaging action. For example, the line of configuration I use here is written like this:

  "scripts": {
    "dist": "pkg . --out-path=dist/ --targets=win --compress=GZip"
  },

pkg .  : It will look for the package.json file in the specified directory, and then look for the bin field as the entry file.

-t or -targets   : Specify the packaged target platform and Node version, such as -t node6-win-x64, node6-linux-x64, node6-macos-x64 can package executable programs for 3 platforms at the same time

-o  : specifies the name of the output executable

--out-path : Specify the output directory

--compress : compressed format after packaging

(3) js and static files

This is more critical. As mentioned earlier, pkg packaging will start from the entrance to find relevant resources based on dependencies, and package them all. However, direct packaging is only limited to require references. If your code uses __dirname splicing The form of the variable must be configured in packge.json.

For example: read views and public files through __dirname in the project

app.set('views',path.join(__dirname, './views'))
app.use(express.static(path.join(__dirname, './public')));

The files in the views and public folders will not be automatically packaged by pkg, and need to be configured:

{ 
    "script": {}, 
    "pkg": { 
        "assets": ["public/**/*","views/**/*"], 
        "scripts": "workers/**/*.js" 
    }, 
}

assets : Indicates the configuration related to static resources, such as public/**/*, which means that all files under public are packaged;

scritps : Indicates js scripts that need to be configured to be packaged

3. Packing

npm run dist

If the packaging is successful, there will be an extra xxx.exe file under the dist file in the project

Five, the pit encountered

1. For the version of node and cache, you can refer to the article of this big guy

pkg package node project as exe_Li Jiang~'s blog-CSDN blog_pkg package

2. Missing files, static file reading failure

pkg only recognizes files loaded by require, __dirname, __filename and process.cwd,

__dirname and __filename must be configured in assets

process.cwd reads external files. For example, the project has a database file that needs to be read. Put the database file and the packaged exe file together and read it through process.cwd

In addition to the above four other writing methods, it cannot be read

__dirname和__filename的区别

__dirname:D://work/service/data
__filename:D://work/service/data/index.js

3. Failed to read the database file

process.cwd reads external files. For example, the project has a database file that needs to be read. Put the database file and the packaged exe file together and read it through process.cwd

6. Summary

        Maybe many friends, like me, do not have deep knowledge of Node, so they will find it difficult. No matter which way (study or work) needs to be touched, be patient. It is not difficult to package pkg, as long as it is configured according to the requirements , it can be successfully packaged and run.


 

Guess you like

Origin blog.csdn.net/m0_55173487/article/details/128015052