使用 pkg 打包 js 程式

把執行檔放到該平台執行時出現 Error: Cannot find module '/snapshot/xxx/node_modules/sqlite3/lib/binding/node-v64-linux-x64/node_sqlite3.node'

查了下才發現跨平台的話會無法自動打包

因為 binary 檔案不同

所以執行前要把該平台的 binary 檔案放到 js 程式同目錄

1.首先要安裝該平台的 binary

npm i sqlite3 --target_platform=linux --target_arch=x64

2.打包 (注意: target node 最好與機器上同版本)

pkg --target=node10-linux-x64 package.json

3.執行 binary (注意: sqlite3 binary 檔需與執行檔放在同一目錄)

cp node_modules/sqlite3/lib/binding/node-v64-linux-x64/node_sqlite3.node .

./main

 

note: node 查詢 platform & arch

[~] # node
> process.platform
'linux'
> process.arch
'x64'

 

 

https://github.com/mapbox/node-sqlite3/wiki/Binaries

https://jingsam.github.io/2018/03/02/pkg.html

https://www.npmjs.com/package/node-pre-gyp

arrow
arrow
    全站熱搜

    JohnDX 發表在 痞客邦 留言(0) 人氣()