即使编译成功,Electron也没有在桌面窗口打开我的react项目



我一直试图打开我的反应项目与Electron。但是,该应用程序可以在浏览器上打开,而不能在windows应用程序中打开。

我的包。json文件——

"main": "public/electron.js",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"dev": "concurrently -k "cross-env BROWSER=none npm start" "npm:electron"",
"electron": "wait-on tcp:3000 && electron ."
},

我的电子。js文件在公共文件夹——

const path = require('path');
const { app, BrowserWindow } = require('electron');
const isDev = require('electron-is-dev');
function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
// and load the index.html of the app.
// win.loadFile("index.html");
win.loadURL(
isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, '../build/index.html')}`
);
// Open the DevTools.
if (isDev) {
win.webContents.openDevTools({ mode: 'detach' });
}
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
app.quit();
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});

我已经安装了所有必需的包,并且编译成功。但是,当我在终端中运行npm run dev时,它没有打开任何窗口。为什么会这样呢?

我也遇到过这个问题。我所做的是在package。json我从这个

更改了代码
"electron": "wait-on tcp:3000 && electron ."

"electron": "electron ."

窗口将打开,但它将是空白的。一旦react构建完成,你必须重新加载窗口。我没有别的方法来解决这个问题。但这是我能分享的最好的。

最新更新