Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set NODE_ENV=production on Electron app when packaged with electron-packager?

How do I make packaged releases of my Electron application set NODE_ENV=production when packaged with electron-packager?

like image 283
Kyle Kelley Avatar asked Sep 07 '16 06:09

Kyle Kelley


1 Answers

UPDATE 2019/12

Use app.isPackaged: https://electronjs.org/docs/api/app#appispackaged-readonly

It returns true if the app is packaged, false otherwise. Assuming you only need a check if it's in production or not, that should do it. The env file solution detailed below would be more suitable if you had different environments/builds with different behaviors.


To my knowledge, you can't pass env vars to a packaged electron app on start (unless you want your users to always start it from the command line and pass it themselves). You can always set that env variable in your application like this: process.env.NODE_ENV = 'production'. You could integrate that with electron-packager by having an env file that gets set in your build and is required by your application to determine what environment it's in.

For example, have a packaging script that looks like:

"package": "cp env-prod.json src/env.json && npm run build"

and in your src/main.js file:

const appEnv = require('./env.json');
console.log(appEnv) //=> { env: "prod", stuff: "hey" }
//you don't really need this, but just in case you're really tied to that NODE_ENV var
if(appEnv.env === 'prod') {
  process.env.NODE_ENV = 'production';
}
like image 105
ccnokes Avatar answered Nov 22 '22 17:11

ccnokes