Search code examples
reactjselectron-packager

Getting electron-packager to use production build


I have a create-react-app and I'm trying to figure out how to get electron-packager to create an .exe using the production build. It keeps using the development folder in ./src

Running

electron-packager . --no-prune --ignore=/node_modules --ignore=/e2e --overwrite --ignore=/src

doesn't work. I also tried

electron-packager ./build --no-prune --ignore=/node_modules --ignore=/e2e --overwrite --ignore=/src

Here are the relevant entries in my package.json

{
    "homepage": "./",
    "main": "src/electron-entry.js",
    "build": {
        "target": "nsis",
        "dir": "./build"
    }
}

Here is my loadURL that points to the production build. In electron-entry.js. This works when I run in a dev environment.

new BrowserWindow(BrowserWindowProps).loadURL(url.format({
    pathname: path.join(__dirname, '/../build/index.html'),
    protocol: 'file:',
    slashes: true
}));

Is the data in electron-entry.js even relevant to the directory electron-packager uses?


Solution

  • Found a solution. I placed my electron entry file into the root directory and changed the loadURL to reflect that.

    let startUrl = process.env.ELECTRON_START_URL || url.format({
        pathname: path.join(__dirname, '/build/index.html'),
        protocol: 'file:',
        slashes: true
    });
    mainWindow.loadURL(startUrl);
    

    Changed my package.json to reflect this change:

    {
      "main": "./electron-entry.js",
    }
    

    Then I ran

    electron-packager . --no-prune --ignore=/node_modules --ignore=/e2e --overwrite --ignore=/src