Problem Description:
I’m encountering an issue with my Electron app. After packaging the app, it displays a blank screen. However, when running it locally with npm run electron:serve, everything works fine.
Code Details:
const { app, BrowserWindow } = require('electron')
const path = require('path')
const isDev = require('electron-is-dev')
require('@electron/remote/main').initialize()
function createWindow() {
// Create the browser window.
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true, // false tried both
enableRemoteModule: true
}
})
win.loadURL(
isDev
? 'http://localhost:3000'
: `file:///Users/avinashkumaranshu/Project/Desktop/react-electron/build/index.html`
)
}
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
import { useState, useMemo } from 'react'
import { FilesViewer } from './FilesViewer'
const fs = window.require('fs')
const pathModule = window.require('path')
const { app } = window.require('@electron/remote')
const formatSize = size => {
var i = Math.floor(Math.log(size) / Math.log(1024))
return (
(size / Math.pow(1024, i)).toFixed(2) * 1 +
' ' +
['B', 'kB', 'MB', 'GB', 'TB'][i]
)
}
function App() {
const [path, setPath] = useState(app.getAppPath())
const files = useMemo(
() =>
fs
.readdirSync(path)
.map(file => {
const stats = fs.statSync(pathModule.join(path, file))
return {
name: file,
size: stats.isFile() ? formatSize(stats.size ?? 0) : null,
directory: stats.isDirectory()
}
})
.sort((a, b) => {
if (a.directory === b.directory) {
return a.name.localeCompare(b.name)
}
return a.directory ? -1 : 1
}),
[path]
)
const onBack = () => setPath(pathModule.dirname(path))
const onOpen = folder => setPath(pathModule.join(path, folder))
const [searchString, setSearchString] = useState('')
const filteredFiles = files.filter(s => s.name.startsWith(searchString))
return (
<div className="container mt-2">
<h4>{path}</h4>
<div className="form-group mt-4 mb-2">
<input
value={searchString}
onChange={event => setSearchString(event.target.value)}
className="form-control form-control-sm"
placeholder="File search"
/>
</div>
<FilesViewer files={filteredFiles} onBack={onBack} onOpen={onOpen} />
</div>
)
}
export default App
{
"name": "electron-react",
"version": "0.1.0",
"private": true,
"dependencies": {
"@electron/remote": "^1.0.2",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"bootstrap": "^5.0.0-beta1",
"electron-is-dev": "^1.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"web-vitals": "^0.2.4"
},
"main": "public/main.js",
"homepage": "./",
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"electron:serve": "concurrently -k "cross-env BROWSER=none yarn start" "yarn electron:start"",
"electron:build": "yarn build && electron-builder -c.extraMetadata.main=build/main.js",
"electron:start": "wait-on tcp:3000 && electron ."
},
"build": {
"extends": null,
"appId": "com.example.electron-cra",
"files": [
"dist/**/*",
"build/**/*",
"node_modules/**/*",
"package.json"
],
"directories": {
"buildResources": "assets"
}
},
"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": "^5.3.0",
"cross-env": "^7.0.3",
"electron": "^16.0.0",
"electron-builder": "^24.9.1",
"prettier": "^2.2.1",
"wait-on": "^5.2.1"
}
}
Additional Information:
- Dependencies:
- electron: 16.0.0
- electron-builder: 24.9.1
- Scripts:
- How you run the app locally (npm run electron:serve).
- How you build and package the app (npm run electron:build).
- Steps Taken:
- I’ve checked the console for errors but didn’t find anything unusual.
- I’ve hardcoded the paths in the main.js file.
Environment:
- Operating System: macOS
- Node.js: 14.21.3