nodejs ffi-napi pkg打包exe, Error: No native build was found for platform 问题解决

1. 新建文件D:\TestRequire\test.js:

const ffi = require("ffi-napi");
var current = ffi.Library('ntdll.dll', {
  atoi: ["int", ["string"]],  
});
console.log("atoi: " + current.atoi("1000"));
2. 安装ffi-napi和pkg

PS D:\TestRequire> npm i ffi-napi

added 8 packages, and audited 159 packages in 15s

18 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

PS D:\TestRequire> npm i pkg

up to date, audited 159 packages in 2s

18 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
PS D:\TestRequire>

3. pkg打包exe文件,生成文件test.exe。

PS D:\TestRequire> npx pkg test.js -t win
> pkg@5.8.0

4.执行test.exe, 发生如下错误。
PS D:\TestRequire> .\test.exe
pkg/prelude/bootstrap.js:1876
throw error;
^

Error: No native build was found for platform=win32 arch=x64 runtime=node abi=108 uv=1 libc=glibc node=18.5.0
loaded from: C:\snapshot\TestRequire\node_modules\ref-napi

at Function.<anonymous> (C:\snapshot\TestRequire\node_modules\node-gyp-build\node-gyp-build.js:60:9)
at load (C:\snapshot\TestRequire\node_modules\node-gyp-build\node-gyp-build.js:22:30)
at Object.<anonymous> (C:\snapshot\TestRequire\node_modules\ref-napi\lib\ref.js:8:53)
at Module._compile (pkg/prelude/bootstrap.js:1930:22)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Module._load (node:internal/modules/cjs/loader:834:12)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at Module.require (pkg/prelude/bootstrap.js:1855:31)
at require (node:internal/modules/cjs/helpers:102:18)

Node.js v18.5.0

5. 修改package.json文件。

{
    "name": "test",
    "version": "1.0.0",
    "description": "",
    "main": "test.js",
    "scripts": {
        "start": "node ./test.js",
        "build": "pkg . --target win"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "ffi-napi": "^4.0.3",
        "pkg": "^5.8.0"
    },
    "bin": "test.js",
    "pkg": {
        "assets": [
            "node_modules/ffi-napi/**/*",
            "node_modules/ref-napi/**/*"
        ]
    }
}
6. npm run build 生成test.exe

PS D:\TestRequire> npm run build

> test@1.0.0 build
> pkg . --target win

> pkg@5.8.0

7.执行exe,成功。
PS D:\TestRequire> .\test.exe
atoi: 1000

posted @ 2023-01-09 16:26  oubin  阅读(1763)  评论(0)    收藏  举报