I use electron-builder to pack my React electron app. After installing and launching my application I’m getting this error:
Error: ENOENT, build\index.html not found in D:\Test\MyApp\resources\app.asar
at createError (node:electron/js2c/asar_bundle:2:1574)
at Object.lstat (node:electron/js2c/asar_bundle:2:4546)
at node:electron/js2c/asar_bundle:2:4950
at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
(I think) This problem caused by my express server, that streams index.html file.
Code, that starts express server using index.html, builded by npm run build:
const startServer = () => {
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(7865);
}
Creating window code:
const createWindow = async () => {
win = new BrowserWindow({
width: 1080,
height: 720,
minWidth: 1350,
minHeight: 860,
title: 'MyApp',
webPreferences: {
nodeIntegration: true,
devTools: true,
contextIsolation: false,
enableRemoteModule: true,
},
})
win.removeMenu()
win.setMenu(null)
isDev ? null : startServer()
isDev ? win.loadURL('http://localhost:3000')
: win.loadURL('http://localhost:7865')
win.maximize()
win.webContents.on('before-input-event', (_, input) => {
if (input.type === 'keyDown' && input.key === 'F12') {
win.webContents.isDevToolsOpened()
? win.webContents.closeDevTools()
: win.webContents.openDevTools({ mode: 'right' });
}
})
}
//const {app, BrowserWindow} = require('electron')
app.whenReady().then(() => {
createWindow()
})
Build field in package.json:
"build": {
"productName": "MyApp",
"appId": "com.test.myapp",
"extends": null,
"win": {
"target": "nsis",
},
"nsis": {
"allowToChangeInstallationDirectory": true,
"oneClick": false,
}
}
directory structure:
https://i.stack.imgur.com/fCxOz.png
I’m sure that problem is in built application. When I use this code in dev mode (with changing isDev -> !isDev) server with index.html works fine, as I expect.
How can it be fixed?