使用 Electron-builder 时出现错误“@serialport ...不是 Win32 应用程序”

问题描述 投票:0回答:1

我有一个应用程序应该从外部秤读取串行输入,为此我制作了一个电子应用程序。 Image of a windows 10 error when starting the app build in linux cuz windows can't built it

我尝试寻找修复方法,但找不到适合我需求的解决方案,或者它们不起作用......

我在 Linux 上工作,但应用程序适用于 Windows 操作系统,有趣的是我可以通过 Linux 构建应用程序,但不能在 Windows 上构建应用程序。

我在 Windows 操作系统上尝试构建时遇到的错误:

C:\Users\Win\Desktop\1.2.5>npm run build
> [email protected] build
> electron-builder --win
• electron-builder  version=24.13.3 os=10.0.19045
• loaded configuration  file=package.json ("build" field)
• @electron/rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies
To ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`
• writing effective config  file=dist\builder-effective-config.yaml
• rebuilding native dependencies  dependencies=@serialport/[email protected] platform=win32 arch=x64
⨯ cannot execute  cause=exit status 1
errorOut=npm error code 1
npm error path C:\Users\Win\Desktop\1.2.5\node_modules\@serialport\bindings-cpp
npm error command failed
npm error command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm error node:internal/child_process:421
npm error     throw new ErrnoException(err, 'spawn');
npm error     ^
npm error
npm error Error: spawn EINVAL
npm error     at ChildProcess.spawn (node:internal/child_process:421:11)
npm error     at Object.spawn (node:child_process:761:9)
npm error     at build (C:\Users\Win\Desktop\1.2.5\node_modules\@serialport\bindings-cpp\node_modules\node-gyp-build\bin.js:29:8)
npm error     at preinstall (C:\Users\Win\Desktop\1.2.5\node_modules\@serialport\bindings-cpp\node_modules\node-gyp-build\bin.js:38:32)
npm error     at Object.<anonymous> (C:\Users\Win\Desktop\1.2.5\node_modules\@serialport\bindings-cpp\node_modules\node-gyp-build\bin.js:15:3)
npm error     at Module._compile (node:internal/modules/cjs/loader:1546:14)
npm error     at Module._extensions..js (node:internal/modules/cjs/loader:1691:10)
npm error     at Module.load (node:internal/modules/cjs/loader:1317:32)
npm error     at Module._load (node:internal/modules/cjs/loader:1127:12)
npm error     at TracingChannel.traceSync (node:diagnostics_channel:315:14) {
npm error   errno: -4071,
npm error   code: 'EINVAL',
npm error   syscall: 'spawn'
npm error }
npm error
npm error Node.js v22.6.0
npm error A complete log of this run can be found in: C:\Users\Win\AppData\Local\npm-cache\_logs\2024-08-19T20_47_08_788Z-debug-0.log

                command='C:\Program Files\nodejs\node.exe' 'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js' rebuild @serialport/[email protected]
                workingDir=
C:\Users\Win\Desktop\1.2.5>

在 Linux 上构建时(构建成功,但应用程序无法启动):

linux-mysistem:~/Desktop/Market App$ npm run build
> [email protected] build
> electron-builder --win
• electron-builder  version=24.13.3 os=6.8.0-40-generic
• loaded configuration  file=package.json ("build" field)
• @electron/rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies
To ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`
• writing effective config  file=dist/builder-effective-config.yaml
• skipped dependencies rebuild  reason=platform is different and buildDependenciesFromSource is set to true
• packaging       platform=win32 arch=x64 electron=31.4.0 appOutDir=dist/win-unpacked
• building        target=nsis file=dist/Market App Setup 1.2.5.exe archs=x64 oneClick=false perMachine=false
• building block map  blockMapFile=dist/Market App Setup 1.2.5.exe.blockmap

另外,这是我使用串口的代码:https://pastebin.com/raw/RJdJGwNu

这是我的package.json:

{
"name": "my-app",
"version": "1.2.5",
"description": "My app description",
"main": "main.js",
"scripts": {
  "start": "electron main.js",
  "dist": "electron-builder",
  "build": "electron-builder --win",
  "pack": "electron-builder --win --dir",
  "postinstall": "electron-builder install-app-deps",
  "rebuild": "electron-rebuild -f -w serialport"
},
"build": {
  "appId": "com.name.app",
  "productName": "My App",
  "buildDependenciesFromSource": true,
  "win": {
    "icon": "assets/icon.png"
  },
  "nsis": {
    "oneClick": false,
    "allowToChangeInstallationDirectory": false,
    "license": "assets/LICENSE.txt",
    "installerIcon": "assets/icon-installer.ico",
    "installerHeaderIcon": "assets/icon-installer.ico",
    "uninstallerIcon": "assets/icon-uninstaller.ico",
    "installerSidebar": "assets/sidebar.png",
    "uninstallerSidebar": "assets/sidebar.png"
  },
  "files": [
    "main.js",
    "package.json",
    "node_modules/**",
    "scripts/**",
    "pages/**",
    "assets/**"
  ]
},
"devDependencies": {
  "@types/electron-prompt": "^1.6.5",
  "@types/fs-extra": "^11.0.4",
  "@types/nedb": "^1.8.16",
  "@types/node-schedule": "^2.1.7",
  "electron": "^31.2.1",
  "electron-builder": "^24.13.3",
  "electron-rebuild": "^3.2.9"
},
"dependencies": {
  "electron-prompt": "^1.7.0",
  "fs-extra": "^11.1.1",
  "nedb": "^1.8.0",
  "node-schedule": "^2.1.1",
  "serialport": "^12.0.0"
}
}
npm electron serial-port electron-builder
1个回答
0
投票

我在节点 v20.17.0 上也遇到了同样的问题。我通过将节点版本降级到 20.11.1 并运行“npm i”来修复它。

© www.soinside.com 2019 - 2024. All rights reserved.