pkg打包node應(yīng)用全指南

node應(yīng)用部署
node應(yīng)用部署的常見(jiàn)方式就是把代碼放服務(wù)器上,然后用命令行執(zhí)行代碼就可以了。比如express框架會(huì)自動(dòng)生成一個(gè)www啟動(dòng)文件,部署時(shí)執(zhí)行這段就可以了:
node ./bin/www
但是這種方式的缺點(diǎn)也很明顯,代碼赤裸裸地放在服務(wù)器上,這對(duì)于軟件交付來(lái)說(shuō)并不是安全可靠的做法。今天就來(lái)介紹一個(gè)node應(yīng)用的打包工具--pkg,非常方便,不過(guò)有許多需要注意的地方,相信對(duì)你有用!
pkg打包node應(yīng)用
這里要說(shuō)的pkg是一個(gè)npm包,可以用來(lái)打包node應(yīng)用為可執(zhí)行文件。想想看,一個(gè)node應(yīng)用被打包成exe文件,這樣丟到服務(wù)器上,即省事又安全。來(lái)看看怎么操作吧!
安裝
可以直接全局安裝pkg,方便打包
npm install -g pkg
pkg入口配置
pkg執(zhí)行打包的原理就是從入口文件開(kāi)始,根據(jù)相關(guān)依賴順藤摸瓜地執(zhí)行打包過(guò)程,顯然這個(gè)入口文件與node應(yīng)用程序的自身是一樣的。在配置文件package.json里,需要加上bin屬性,專門(mén)指定pkg的執(zhí)行入口,入口可以有多種方式:
// package.json
{
...
"bin": "./bin/www",
"bin": "./service.js"
...
}
pkg執(zhí)行配置
pkg的用法可以看下官方文檔[1],在命令行輸入pkg -h 也可以看到相關(guān)指導(dǎo)說(shuō)明
pkg [options] <input>
Options:
-h, --help output usage information
-v, --version output pkg version
-t, --targets comma-separated list of targets (see examples)
-c, --config package.json or any json file with top-level config
--options bake v8 options into executable to run with them on
-o, --output output file name or template for several files
--out-path path to save output one or more executables
-d, --debug show more information during packaging process [off]
-b, --build don't download prebuilt base binaries, build them
--public speed up and disclose the sources of top-level project
--public-packages force specified packages to be considered public
--no-bytecode skip bytecode generation and include source files as plain js
-C, --compress [default=None] compression algorithm = Brotli or GZip
Examples:
– Makes executables for Linux, macOS and Windows
$ pkg index.js
– Takes package.json from cwd and follows 'bin' entry
$ pkg .
– Makes executable for particular target machine
$ pkg -t node14-win-arm64 index.js
– Makes executables for target machines of your choice
$ pkg -t node12-linux,node14-linux,node14-win index.js
– Bakes '--expose-gc' and '--max-heap-size=34' into executable
$ pkg --options "expose-gc,max-heap-size=34" index.js
– Consider packageA and packageB to be public
$ pkg --public-packages "packageA,packageB" index.js
– Consider all packages to be public
$ pkg --public-packages "*" index.js
– Bakes '--expose-gc' into executable
$ pkg --options expose-gc index.js
– reduce size of the data packed inside the executable with GZip
$ pkg --compress GZip index.js
pkg執(zhí)行是最關(guān)鍵的打包動(dòng)作,比如我這里用到的這行配置是這樣寫(xiě)的:
// package.json
{
"scripts"{
"pkg":"pkg . -t node12-win-x64 --out-path=dist/"
}
}
結(jié)合官方文檔內(nèi)容,我們來(lái)看下這行代碼中這些參數(shù)的意思:
1、 pkg .,意思就是它會(huì)尋找指定目錄下的package.json文件,然后再尋找bin字段作為入口文件。
2、-t 用來(lái)指定打包的目標(biāo)平臺(tái)和Node版本,如-t node12-win-x64,node12-linux-x64,node12-macos-x64,可以同時(shí)打包3個(gè)平臺(tái)的可執(zhí)行程序;
3、--out-path 用來(lái)指定輸出的目錄地址;后面的"=dist/"就是指定的目錄地址,也可以這樣寫(xiě)"--out-path dist/",用空格替代"="
靜態(tài)資源配置
這塊較關(guān)鍵,前面提到pkg打包會(huì)從入口開(kāi)始根據(jù)依賴去找相關(guān)資源,并把這些都打包進(jìn)去,不過(guò)直接打包這種情況僅限于require引用方式,如果你的代碼中有用到__dirname拼接變量的形式,就要在packge.json中進(jìn)行配置了。
比如我的代碼中有這樣兩行代碼:
app.set('views',path.join(__dirname, './views'))
...
app.use(express.static(path.join(__dirname, './public')));
那views與public文件夾中的文件都不會(huì)被pkg自動(dòng)打包,需要進(jìn)行配置:
{
"script": {},
"pkg": {
"assets": ["public/**/*","views/**/*"],
"scripts": "workers/**/*.js"
},
}
要注意的是像public/**/* 這種通配符寫(xiě)法,表示public下所有文件都被打包進(jìn)去了; assets表示靜態(tài)資源相關(guān)配置,scritps表示需要配置才能打包的js腳本;
至此,打包的相關(guān)配置基本完成,完整的配置文件可以看下:
{
"name": "pngservice",
"version": "0.0.0",
"private": true,
"bin": "./bin/www",
"scripts": {
"start": "node ./bin/www",
"restart": "pm2 start ./bin/www --watch",
"pkg": "pkg . -t node12-win-x64 --out-path dist/",
"pkgout": "pkg . -t node12-win-x64 --out-path D:/ --dist"
},
"pkg": {
"assets": ["public/**/*","views/**/*"],
"scripts": "workers/**/*.js"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "^3.1.6",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"mime": "^2.5.2",
"morgan": "~1.9.1",
"mssql": "^6.2.3",
"pngjs": "^6.0.0",
"request": "^2.88.2",
"tedious": "^9.2.1"
},
"devDependencies": {
"babel-preset-env": "^1.7.0",
"babel-register": "^6.26.0"
}
}
外置配置文件
有時(shí)會(huì)有這種需求,我需要把配置文件放在外面,不用打包進(jìn)exe中,方便部署時(shí)修改相關(guān)配置。這時(shí)就需要用到process.cwd了,具體如下:
// 配置文件會(huì)打包到執(zhí)行文件exe中
var config = require("../config/cfg.json")
// 配置文件不會(huì)被打包,文件exe與config目錄需要整體部署
const fs = require("fs");
const path = require("path");
var config = null;
fs.readFileSync(path.join(process.cwd(), '../config/config.json'), callback)
function callback(err, data){
config = JSON.parse(data)
}
需要注意的是這里最好用fs.readFileSync同步寫(xiě)法,而不是fs.readFile異步寫(xiě)法,這樣保證后續(xù)代碼在用到config值時(shí)不會(huì)報(bào)錯(cuò),不然打包會(huì)出錯(cuò)。
遇到的問(wèn)題
我在打包時(shí)遇到很多問(wèn)題,寫(xiě)文章時(shí)已經(jīng)把坑填上了,按照前面的內(nèi)容操作基本不會(huì)有啥問(wèn)題。不過(guò)我在打包時(shí)遇到這個(gè)問(wèn)題:
非常的詭異,后來(lái)在找到代碼中用到env變量的地方,在www文件下有這樣一段代碼:
require('babel-register')({
presets: ['env']
});
'babel-register'改寫(xiě)了 node 本身的 require,添加鉤子,然后在 require 其他模塊的時(shí)候,就會(huì)觸發(fā) babel 編譯,即實(shí)時(shí)編譯。也就是說(shuō)引入 require('babel-register') 的文件代碼,是不會(huì)被編譯的。這就導(dǎo)致我用pkg打包時(shí),www文件無(wú)法被編譯到exe中。于是我果斷把這段代碼給注釋了,表示我需要此文件被編譯,然后就正常了。
//require('babel-register')({
// presets: ['env']
//});
好了,文章到此結(jié)束,如果對(duì)你有用請(qǐng)記得點(diǎn)贊、分享、在看哦,謝謝鼓勵(lì)!
參考資料:
https://www.jianshu.com/p/5aa149e16ab9 https://juejin.cn/post/6844903937057751054 https://juejin.cn/post/6844903569062117384 https://www.cnblogs.com/mordom/archive/2018/01/23/8337337.html https://blog.csdn.net/u012211003/article/details/112872859 https://www.cnblogs.com/mengff/p/9753867.html
掃碼關(guān)注 字節(jié)逆旅 公眾號(hào),為您奉獻(xiàn)更多技術(shù)干貨!
