I'm using the npm package
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:
I don't see the use of an extra environment variable, when you set the NODE_ENV on startup... Unless there is a pelling reason not to, I will just check on process.env.NODE_ENV
as I'm used to.
I'm using the npm package https://github./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./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 pelling reason not to, I will just check on process.env.NODE_ENV
as I'm used to.
- By dev tools do you mean the inspection panel in the electron window, or do you mean something else. – Ameer Commented Nov 24, 2019 at 15:26
- @ameer I don’t see where I mentioned devTools... – Trace Commented Nov 24, 2019 at 15:32
3 Answers
Reset to default 8I 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?
You can use electron builtin variable for this that is app.isPackaged It can be found here https://www.electronjs/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/manage-staging-environments-in-electronjs/
I have noticed that the process.env
variable in development contains information about npm_package which is absent in production build.
Checking for process.env.npm_node_execpath
gives me info about prod or dev.