Nodejs使用pkg打包為可執(zhí)行文件的解決方案
安裝pkg
npm install -g pkg
查看pkg命令
pkg --help
修改package.json
新增bin入口配置
{
"name": "takescreenshot",
"version": "1.0.0",
"bin": "app.js", // 新增bin入口配置
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^4.21.1",
}
}打包
默認打包
pkg .
如果上面package.json沒有配置bin的話,可以執(zhí)行
pkg .\app.js
指定打包的文件名
pkg . --target win --output 你的文件名.exe
報錯解決
PS E:\TakeProject\takeScreenShot> pkg .
> pkg@5.8.1
> Targets not specified. Assuming:
node16-linux-x64, node16-macos-x64, node16-win-x64
> Fetching base Node.js binaries to PKG_CACHE_PATH
fetched-v16.16.0-win-x64 [ ] 0%> Not found in remote cache:
{"tag":"v3.4","name":"node-v16.16.0-win-x64"}
> Building base binary from source:
built-v16.16.0-win-x64
> Fetching Node.js source archive from nodejs.org...
> Error! AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:(0, assert_1.default)(!this.bar)
at Log.enableProgress (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\log.js:64:30)
at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:58:31
at step (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:33:23)
at Object.next (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:14:53)
at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:8:71
at new Promise (<anonymous>)
at __awaiter (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:4:12)
at downloadUrl (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\utils.js:53:12)
at D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\build.js:142:66
at step (D:\ruanjian\node\nvm\v16.20.2\node_modules\pkg\node_modules\pkg-fetch\lib-es5\build.js:44:23)
進入網站: https://github.com/vercel/pkg-fetch/releases
找到我報錯的tag的版本,點進這個版本的詳情

找到報錯的對象的name值里面的版本號信息
例如上面我的報錯的版本是16.16.0,那就在網站上面找到這個版本

下載報錯的name的名稱對應文件
我報錯的是{"tag":"v3.4","name":"node-v16.16.0-win-x64"}
則在這里找到node-v16.16.0-win-x64點擊下載
下面的v3.4是上面報錯的對象的tag屬性的值
找到C:\Users\22560\.pkg-cache\v3.4文件夾
將上面下載的文件放到文件夾內,并重命名為fetched-v16.16.0-win-x64
同理:linux和macos下載也是一樣,復制到該文件夾內然后將前綴node改成fetched即可
然后重新pkg .即可
到此這篇關于Nodejs使用pkg打包為可執(zhí)行文件的文章就介紹到這了,更多相關nodejs使用pkg打包內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Electron調用外接攝像頭并拍照上傳實現(xiàn)詳解
這篇文章主要為大家介紹了Electron調用外接攝像頭并拍照上傳實例實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Nodejs 和 Electron ubuntu下快速安裝過程
本文較為詳細的給大家介紹了Nodejs 和 Electron ubuntu下快速安裝過程,非常不錯,具有一定的參考借鑒價值,感興趣的朋友跟隨腳本之家小編一起學習吧2018-05-05
iOS + node.js使用Socket.IO框架進行實時通信示例
本篇文章主要介紹了iOS + node.js使用Socket.IO框架進行實時通信示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04

