I've created a test react project with the npx create react app and added the electron.js file. I then run the build script and it was compiled successfully but when I try to run the .exe nothing shows and the console returns a Failed to load resource: net::ERR_FILE_NOT_FOUND.
electron.js
const { app, BrowserWindow } = require("electron");
const path = require("path");
const isDev = require("electron-is-dev");
function createWindow() {
const mainWindow = new BrowserWindow({
width: 1920,
height: 1080,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadURL(
isDev
? "http://localhost:3000"
: `file://${path.join(__dirname, "../build/index.html")}`
);
mainWindow.webContents.openDevTools();
}
app.whenReady().then(() => {
createWindow();
app.on("activate", function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on("window-all-closed", function () {
if (process.platform !== "darwin") app.quit();
});
EDIT: so I tried again from the start and managed to build with just the create-react-app however I've now changed some files with a Routes component and while in dev works fine when I build it nothing shows. console.log are shown but the component isn't.
routes.js
const routes = [
{
exact: true,
path: "/",
component: lazy(() => import("../test")),
},
];
const renderRoutes = (routes) =>
routes ? (
<Suspense fallback={<div>Loading...</div>}>
<Switch>
{routes.map((route, i) => {
const Layout = route.layout || Fragment;
const Component = route.component;
console.log({ Component });
return (
<Route
key={i}
path={route.path}
exact
render={(props) => (
<Layout>
{route.routes ? (
renderRoutes(route.routes)
) : (
<Component {...props} />
)}
</Layout>
)}
/>
);
})}
</Switch>
</Suspense>
) : null;
const Routes = () => {
return renderRoutes(routes);
};
export default Routes;
Problem might be in the fact, that you have changed name for one of the main files, for example electron searches for special filename when building. Please check your filenames.
This error means that file was not found. Either path is wrong or file is not present where you want it to be.