I have a problem with electronjs app with reactjs using webpack-dev-server. I trying to
npm start
that setup and in console application is starting succesfuly but application window does not showing up and even if i run http://localhost:8080/ still nothing.
My file configuration and setup:
webpack.dev.config.js
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.css$/i,
use: ["style-loader", "css-loader"],
},
{
test: /\.(?:js|mjs|cjs)$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [["@babel/preset-env", { targets: "defaults" }]],
},
},
},
{
test: /\.(jpe?g|png|gif)$/,
use: [{ loader: "file-loader?name=img/[name]__[hash:base64:5].[ext]" }],
},
{
test: /\.(eot|svg|ttf|woff|woff2)$/,
use: [
{ loader: "file-loader?name=font/[name]__[hash:base64:5].[ext]" },
],
},
],
},
target: "electron-renderer",
plugins: [
new HtmlWebpackPlugin({ title: "React Electron App" }),
new webpack.DefinePlugin({
"process.env.NODE_ENV": JSON.stringify("development"),
}),
],
devtool: "cheap-source-map"
};
package.json scripts:
"scripts": {
"prod": "cross-env NODE_ENV=production webpack --mode production --config webpack.build.config.js && electron --noDevServer .",
"start": "cross-env NODE_ENV=development webpack-dev-server --hot --host 0.0.0.0 --config=./webpack.dev.config.js --mode development",
"build": "cross-env NODE_ENV=production webpack --config webpack.build.config.js --mode production",
"package": "npm run build",
"postpackage": "electron-packager ./ --out=./release-builds"
},
main.js
const path = require("path");
const url = require("url");
const { app, BrowserWindow } = require("electron");
let mainWindow;
let isDev = false;
if (
process.env.NODE_ENV !== undefined &&
process.env.NODE_ENV === "development"
) {
isDev = true;
}
function createMainWindow() {
mainWindow = new BrowserWindow({
width: 1100,
height: 800,
show: isDev ? true : false,
backgroundColor: "white",
icon: `${__dirname}/assets/icon.png`,
webPreferences: {
nodeIntegration: true,
contentSecurityPolicy: "default-src 'self'",
},
});
let indexPath;
if (isDev && process.argv.indexOf("--noDevServer") === -1) {
indexPath = url.format({
protocol: "http:",
host: "localhost:8000",
pathname: "index.html",
slashes: true,
});
} else {
indexPath = url.format({
protocol: "file:",
pathname: path.join(__dirname, "dist", "index.html"),
slashes: true,
});
}
mainWindow.loadURL(indexPath);
// Don't show until we are ready and loaded
mainWindow.once("ready-to-show", () => {
mainWindow.show();
// Open devtools if dev
if (isDev) {
const {
default: installExtension,
REACT_DEVELOPER_TOOLS,
} = require("electron-devtools-installer");
installExtension(REACT_DEVELOPER_TOOLS).catch((err) =>
console.log("Error loading React DevTools: ", err)
);
mainWindow.webContents.openDevTools();
}
});
mainWindow.on("closed", () => (mainWindow = null));
}
app.on("ready", createMainWindow);
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (mainWindow === null) {
createMainWindow();
}
});
// Stop error
app.allowRendererProcessReuse = true;
eventually you can take full code from here: simple-electronjs-boilerplate
documentation of webpack dev server, webpack configuration, chatgpt, google
startscript doesn't call theelectroncommand.