Your location:Tech News>News>Script>_Pkg use experience to share-based node packed executable files tool

Latest News

_Pkg use experience to share-based node packed executable files tool



  project address


  This project is amazing, directly to the node.js project packaged into exe file windows that can be performed directly (also supports FreeBSD, linux, macos, arm system), you do not even need to install Node.js, and no need to change any code in your project!

  First install pkg

  npm install -g pkg

  Then perform under the project directory

  pkg entrance.js

  You can pack up linux, macos, win3 a platform for executable files.entrance.js file your node for the entrance of the project.

  If you want to pack under the windows exe, then add -t that is packaged into exe files in windows platform, specific optional parameters refer to the project directory

  pkg -t win entrance.js

  After a few moments will be generated under the project directory prepackaged entrance.EXE.

  pkg automatically and fully packed into the file to start looking from the entrance dependent files, without having to modify any code in the project.


  pkg according to package.Json packaged configuration for the default file file entry point to the bin.

  carried out

  pkg .


  pkg package.json

  Automatically in accordance with the package.json configuration package.

  // package.json


  // other configuration items

  "Bin": "service.js ", // entry file

  "Pkg": {

  "Scripts": [

  "Build *.js "http: // need to package other js files come in, you can add multiple


  "Assets": [

  "Dist *" http: // static files directory, you can add more




  Note: Static files need to be replaced in the file referenced in the project

  path.join (__ dirname, ''dist'')

  The form can only be properly packaged, or may not read.


  Use vue-cli build the project, and use the build will compile your project file to generate static dist directory npm run.These are vue-cli own content, not repeat them.

  Create a new service under the project directory.js file and add the following code to express static from a local server, so you can visit your local website (deployment is similar to the line)


  // service.js

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

  const app = express ();

  const path = require ( ''path'');

  app.use (express.static (path.join (__ dirname, ''dist''))); // Note the use of path.join (__ dirname, ''dist'') instead of ''dist'', although execute the command line, the effect is the same, but the pkg package will not recognize the dist directory

  var server = app.listen (8081, function () {

  var host = server.address ().address

  var port = server.address ().port

  console.log ( `AIbuy agents server start successfully on http: // $ {host}: $ {port}`)


  At this point you can execute in the console

  node service.js

  To start your server, and after startup is complete, the browser visit http: // localhost: 8081 / to view your website.

  Next we use the service.js dist directory and packaged into a exe file for others to use

  First install pkg

  npm install -g pkg

  Then modify the package.json, add a bin (if not the service.js words) and pkg items


  // other configuration items

  "Bin": "service.js ", // specify the entry file

  "Pkg": {

  "Assets": [

  "Dist *" http: // Specify packaged static files directory




  Then perform under the project directory

  pkg -t win package.json

  Once completed generates an exe file, double-click the Start which is equivalent to execution node service.js, then your browser (http: // localhost: 8081 /) will be able to access the packaged item!No need to install the node in advance, to demonstrate the boss is very convenient with wood there!

  Based on the above this node packed executable files tool _Pkg use experience to share is small series to share the entire contents of everyone, and I hope to give you a reference, I hope you will support script Home.

  You may also be interested in the article: using pkg Packaging Node.The method applied in step js node app specific use packaging tool pkg

Recommend article

Relate article