我有一个 Electron 应用程序,可以在 VS Code dev 上正常运行
npm run dev
,但是当我使用 electron-builder 发布应用程序并运行 .exe
文件夹中的 dist
文件时,我收到以下错误:
package.json
"name": "jokes_collection_fe",
"version": "0.1.0",
"private": true,
"main": "public/electron.js",
"dependencies": {
"@emotion/react": "^11.5.0",
"@emotion/styled": "^11.3.0",
"@mui/icons-material": "^5.1.0",
"@mui/material": "^5.1.0",
"@testing-library/jest-dom": "^5.15.0",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.24.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "^7.2.6",
"react-scripts": "4.0.3",
"react-toastify": "^8.1.0",
"redux": "^4.1.2",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.0",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "concurrently -k \"SET BROWSER=none&&npm start\" \"npm:electron\"",
"electron": "wait-on tcp:3000 && electron .",
"pack": "electron-builder --dir",
"dist": "electron-builder"
},
"build": {
"appId": "Jokes Collection",
"extends": null,
"win": {
"target": "nsis",
"icon": "build/icon.png"
}
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"concurrently": "^6.4.0",
"electron": "^16.0.4",
"electron-builder": "^22.14.5",
"electron-is-dev": "^2.0.0",
"wait-on": "^6.0.0"
}
}
公共/Electron.js
const path = require("path")
const { app, BrowserWindow } = require("electron")
const isDev = require("electron-is-dev")
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
})
win.loadURL(
isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "../build/index.html")}`
)
if (isDev) {
win.webContents.openDevTools({ mode: "detach" })
}
}
app.whenReady().then(createWindow)
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createW`enter code here`indow()
}
})
在项目目录中运行以下命令: npm 安装 electronics-is-dev
重建应用程序: npm 运行构建
然后再次打包Electron应用程序: npm 运行 dist
它对我有用