我已经阅读了很多有关此问题的文章,但对我来说仍然存在。
我正在构建React Web应用程序,并希望在桌面应用程序中显示它。
这是我的环境版本:
Node : v10.13.0
Npm : 6.4.1
yarn : 1.12.3
起初,我尝试在主项目中与我的npm一起安装电子,但出现错误。因此,我通过powershell使用create-react-app创建了一个新的react项目:
npm install -g create-react-app
然后我通过纱安装电子。我已经将所有要求添加到package.json和main.js中。下面是package.json和main.js
Package.Json:
{
"name": "chat",
"version": "0.1.0",
"main": "public/main.js",
"private": true,
"dependencies": {
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-scripts": "2.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron-start": "electron ."
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"electron": "^3.0.10"
}
}
main.js
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const Menu = electron.Menu;
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({width: 900, height: 680});
mainWindow.loadURL('http://localhost:3000');
app.setAboutPanelOptions({
applicationName: "chat",
applicationVersion: "0.0.1",
})
mainWindow.on('closed', () => mainWindow = null);
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
此后,我通过脚本运行本地主机,开始访问localhost。然后运行yarn run electron-start
打开电子应用,但出现错误:找不到电子应用。