Based on Node packaging executable tool _PKG use experience sharing

project addresses

基于node打包可执行文件工具_Pkg使用心得分享

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

First, mounting pkg

  npm install -g pkg  

and then executed in the project directory

  pkg entrance.js  

can be packaged linux, macos, win3 platforms the executable file. entrance.js for the entry file your node project.

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

  pkg -t win entrance.js  

after a short wait at the project directory will generate packaged entrance.exe file.

pkg will automatically start looking from the entrance dependent files and full packaged in, without having to modify any code in the project.

Other

The PKG may be packaged configuration for package.json, default file entry point to the bin file.
performed
  PKG.  

or

   
can be automatically packaged according to Package.json.

// package.json {// Other configuration item “bin”: “service.js”, // Entrance file “PKG”: {“scripts “: [” Build / ** / *. js “// other JS files that need to be packaged, add multiple],” assets “: [” DIST / ** / * “// Static file directory, Add multiple]}}
Note: Static files need to be replaced with reference to files in the project to
   
Path.join (__ DIRNAME, ‘Dist’)

can be packaged normally, otherwise it may be read.
   
Use the Vue-CLI to establish a project and use NPM Run Build to generate a static file to the dist directory using NPM Run Build. These are the contents of the Vue-CLI, and will not be described again.

New service.js files in the project directory, add the following code, set up a version of Express static servers locally, enable you access your website locally (similar to the online)

// service.jsconst express = required (‘express’); const App = express (); const path = required (‘Path’ App. ISE (Express.Static (path.join)))))); // Note that this is used to use path.join (__ DIRNAME, ‘DIST’) instead of ‘Dist’, although the effect is the same in the command line, but PKG Packing will not recognize the dist of the Dist directory VAR Server = app.listen (8081, function () {var host = server.address (). Address var port = server.address (). Port console.log (`aibuy agent server start surcessfully ON http: // $ {host}: $ {port} `}

At this point you can execute

基于node打包可执行文件工具_Pkg使用心得分享

   
After you start your server, after the startup is complete, the browser accesses http: // localhost: 8081 / Your website.

Next we use the service.js and dist catalogs into an exe file, convenient for others

First install PKG
   

then modify package.json, add bin (if not Service.js) and PKG item

{// Other Configuration Item “BIN”: “service.js”, // Specify the entry file “PKG”: {“Assets”: [“Dist / ** / *” / / Specify to packageStatic file directory]}}
   

pkg -t win package.json

 After completion, generate an EXE file, double-click to start, is equivalent to executing Node Service.js, then you can enter your browser (http: // localhost: 8081 /) Visit the packaged project! You don't need to install Node in advance, and give the boss's demo very convenient to have wood!   This article is based on Node packaging executable tool _PKG use experience sharing is the full content of Xiaobian sharing, I hope to give you a reference, I hope everyone supports Tumi Cloud. 
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment