Delta
Delta

Reputation: 43

Electron app does not function properly after built into an exe file

I have just finished working on a test project in electron. The problem is that when I package the app using an electron-packager into an executable, my main.html file does not load up in the window. However, when I run with npm start it works absolutely fine. I have checked if there is any mistake in the path to my files but that's absolutely fine as well.

Here's my package.json file -

{
  "name": "test",
  "version": "1.0.0",
  "description": "Just a test app",
  "main": "src/main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "Ray",
  "license": "MIT",
  "devDependencies": {
    "electron": "^9.0.5"
  },
  "dependencies": {
    "electron-packager": "^15.0.0"
  }
}

Here is my main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {
  // Create the browser window.
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  win.loadFile('./app/main.html')
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(createWindow)

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On macOS 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()
  }
})

Any help will be appreciated. Thanks.

Upvotes: 1

Views: 1222

Answers (1)

Torf
Torf

Reputation: 1244

First of all electron-packagershould be part of devDependencies, not dependencies.

Second: How do you pack? If you use an asar File you need to look at the Paths. Those change when packing. So I'd recommend you use something like

win.loadURL(`file://${__dirname}/main.html`)

or

win.loadURL(url.format({
    pathname: path.join(__dirname, 'main.html'),
    protocol: 'file:',
    slashes: true
}));

when loading your html.

Upvotes: 1

Related Questions