Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron run in production mode

Tags:

I'm using the npm package https://github.com/sindresorhus/electron-is-dev

For some reason, isDev always returns true.

My npm script looks as follows:

"start:prod": "cross-env NODE_ENV=production && electron dist/main.js"

main.js:

import isDev from 'electron-is-dev';

app.on('ready', () => {
  console.log('isDev', isDev);
  if (!isDev) {
    const {session} = require('electron');
    session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
      callback({responseHeaders: `default-src http: ws:`})
    })
  }
  const win = createWindow();
  createMenu(win);
});

The console outputs:

isDev true

Documentation mentions:

You can force development mode by setting the ELECTRON_IS_DEV environment variable to 1.

But I don't feel that putting the variable to zero should be necessary.

Outputting process.env.ELECTRON_IS_DEV logs undefined.

I found the following thread that I found confusing: https://github.com/electron/electron/issues/7714

I don't see the use of an extra environment variable, when you set the NODE_ENV on startup... Unless there is a compelling reason not to, I will just check on process.env.NODE_ENV as I'm used to.

like image 555
html_programmer Avatar asked Nov 24 '19 15:11

html_programmer


2 Answers

I don't use that npm package but I'll share what I do – (no idea if it is right or wrong but it works. I use it to run electron-reload during development).

package.json – set an env var in my 'start' script

  "scripts": {
    "start": "APP_DEV=true electron ."
   }

main.js - check for the env var in "main.js"

var isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == "true") : false;

use it

if (isDev) {
    require('electron-reload')(__dirname, {
        electron: upath.toUnix(upath.join(__dirname, 'node_modules', '.bin', 'electron'))
    });
}

UPDATE: 4.28.20

Had to bring my project over to a Windows machine to work on some Windows-specific issues and the method above does not work there. Here is my question on that and an answer which gets it working on Windows: Setting an env var in package.json for use with electron-reload?

like image 96
spring Avatar answered Nov 15 '22 05:11

spring


You can use electron builtin variable for this that is app.isPackaged It can be found here https://www.electronjs.org/docs/api/app#appispackaged-readonly Add the below line to check it yourself

console.log(app.isPackaged);

Else

If you are using Electron-is-dev then use

"start": "set ELECTRON_IS_DEV=0 && electron ."

Which will set the environment to production. And for setting it back to DEV replace that 0 with 1.

References - https://www.geeksforgeeks.org/manage-staging-environments-in-electronjs/

like image 23
shivam singh Avatar answered Nov 15 '22 05:11

shivam singh