SoFunction
Updated on 2025-04-03

About the pitfalls of electronic packaging

First of all, there are two ways to package electron

  • By electron-builder
  • By electron-package

In two ways, electron-builder packages are more extensible and have smaller packages. The packages produced by electron-package are relatively less extensible and have a larger package capacity. Generally, electron-builder is chosen for packaging.

Pay attention to the issues with electron and electron-build versions

1. Use both to match. If the packages of both are not matched and the version does not match, it will cause the packaging to fail. Therefore, when we use electron-builder to package, we must pay attention to the matching of the version.
2. Secondly, you should also pay attention to the location of the file where our source code is located. Do not appear in Chinese when the path including the file name, otherwise it will also lead to failure.
3. Network problems are also a relatively pitfall. It is best to use means to make the network smoother, try to use yarn and npm to install dependency packages. Although cnpm is also OK, there is still a difference between cnpm and npm yarn. The package structures they load are different, so it is recommended to use yarn add. Official recommendations are also yarn
4. When the network is not smooth, it is more difficult for us to download electron and electron-builder. I also use the root directory to create a .npmrc file configuration image to operate
and electron-builder are development-time dependencies, especially electron-builder, must be installed under devDependencies.

document

{
  "name": "music-react",
  "version": "0.1.0",
  "private": true,
  "description": "create-react-app electron electron-builder dotcoo test",
  "author": "YJ",
  "main": "public/",
  "homepage": ".",
  "dependencies": {
    "@ant-design/icons": "^4.2.2",
    "@craco/craco": "^5.7.0",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "antd": "^4.7.0",
    "axios": "^0.20.0",
    "immer": "^7.0.14",
    "": "^8.0.1",
    "prop-types": "^15.7.2",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-redux": "^7.2.1",
    "react-router-config": "^5.1.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "3.4.3",
    "redux": "^4.0.5",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0",
    "styled-components": "^5.2.0"
  },
  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject",
    "electron": "cross-env NODE_ENV=development electron .",
    "pack": "cross-env NODE_ENV=production yarn run build && electron-builder --dir",
    "dist": "cross-env NODE_ENV=production yarn run build && electron-builder"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "cross-env": "^7.0.2",
    "electron": "^7.0.0",
    "electron-builder": "^22.4.0"
  }
}

.npmrc file

ELECTRON_MIRROR=/mirrors/electron/
ELECTRON_BUILDER_BINARIES_MIRROR=/mirrors/electron-builder-binaries/

electron main process file

//Introduce moduleconst url = require('url');
const path = require('path');
const { app, BrowserWindow } = require('electron');

// Development environmentconst isDev = .NODE_ENV === 'development';

// Keep a global reference to the window object, if not, when the JavaScript object is// When garbage collection, the window object will be automatically closedlet win = null;

function createWindow () {
  // Create a browser window.  win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      // Use preload preload module to disable nodeIntegration, node can be accessed during the preload stage.      // This is done because even if node is enabled, webpack will not recognize node modules and electron modules when packaging.      // nodeIntegration: true,
      // Use proload to preload the injected module, see the file below      preload: (__dirname, './'),
    },
  });

  // Delete menu  // ();

  // Load the file  (isDev ? 'http://localhost:3000' : ({
    protocol: 'file',
    slashes: true,
    pathname: (__dirname, ''),
  }));

  // Open the developer tools  isDev && ();

  // When window is closed, this event will be triggered.  ('closed', () => {
    // Dereference window objects, if your application supports multiple windows,    // Usually multiple window objects are stored in an array.    // At the same time, you should delete the corresponding element.    win = null;
  });
}

// Electron will be initialized and prepared// Call this function when creating a browser window.// Some APIs can only be used after the ready event is triggered.('ready', createWindow);

// Exit when all windows are closed.('window-all-closed', () => {
  // On macOS, unless the user exits with Cmd + Q,  // Otherwise most applications and their menu bars will remain active.  if ( !== 'darwin') {
    ();
  }
});

('activate', () => {
  // On macOS, when the dock icon is clicked and no other window opens,  // Usually recreate a window in the application.  if (win === null) {
    createWindow();
  }
});

// In this file, you can continue to write the remaining main process code of the application.// It can also be split into several files,Then use require Import。

document

// Here is a preloaded file. You can use the node module even if nodeIntegration is not enabled.// We add the required module to the window, and then we can get the required module from the window object on the page. = require('electron');
 = require('fs');
 = require('express');

This is the basic packaging step. Here, if the project I packaged here is a react project, vue, there are similarities. The most important thing is to avoid the pitfalls of unsuitable versions and poor network connections.

This is the end of this article about the pitfalls of electron packaging. For more related electron packaging content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!