How to set NODE_ENV = production on the Electron application when packaging with an electronic packer?

How to make packaged releases of my Electron set app NODE_ENV=productionwhen packing with electron-packager?

+4
source share
2 answers

As far as I know, you cannot transfer env vars to a packaged electronic application at startup (unless you want your users to always launch it from the command line and transfer it themselves). You can always set this env in your application as follows: process.env.NODE_ENV = 'production'. You can integrate this with the electronic packer by having an env file that is installed in your assembly and is required by your application to determine which environment it is in.

For example, you have a script package that looks like this:

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

and in your file src/main.js:

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';
}

However, I think the easiest way would be to check electron-prebuiltinprocess.execPath

const isProd = process.execPath.search('electron-prebuilt') === -1;
+2
source

You can install it in two ways.

--no-prune . guide

API

var packager = require('electron-packager');
var options = {
  'arch': 'ia32',
  'platform': 'win32',
  'dir': './'
  'prune': true //this set the enviroment on production and ignore dev modules
};
packager(options, function done_callback (err, appPaths) { /* … */ })

.

0

Source: https://habr.com/ru/post/1653871/


All Articles