question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Package with process.env variables set

See original GitHub issue

In my main.js file, I have process.env variables set that I need to package and have them set when you open the packaged app. How can I do this? Here is my package.js file:

/* eslint strict: 0, no-shadow: 0, no-unused-vars: 0, no-console: 0 */
'use strict';

const os = require('os');
const webpack = require('webpack');
const cfg = require('./webpack/app.production.js');
const packager = require('electron-packager');
const del = require('del');
const exec = require('child_process').exec;
const argv = require('minimist')(process.argv.slice(2));
const pkg = require('./package.json');
const devDeps = Object.keys(pkg.devDependencies);

const appName = argv.name || argv.n || pkg.productName;
const shouldUseAsar = argv.asar || argv.a || false;
const shouldBuildAll = argv.all || false;

console.log(process.env.GOOGLE_APP_CLIENT);

const DEFAULT_OPTS = {
  dir: './',
  name: appName,
  asar: shouldUseAsar,
  ignore: [
    '/test($|/)',
    '/tools($|/)',
    '/release($|/)'
  ].concat(devDeps.map(name => `/node_modules/${name}($|/)`))
};

const icon = argv.icon || argv.i || 'app/app';

if (icon) {
  DEFAULT_OPTS.icon = icon;
}

const version = argv.version || argv.v;

if (version) {
  DEFAULT_OPTS.version = version;
  startPack();
} else {
  // use the same version as the currently-installed electron-prebuilt
  exec('npm list electron-prebuilt', (err, stdout) => {
    if (err) {
      DEFAULT_OPTS.version = '0.36.2';
    } else {
      DEFAULT_OPTS.version = stdout.split('electron-prebuilt@')[1].replace(/\s/g, '');
    }

    startPack();
  });
}


function startPack() {
  console.log('start pack...');
  webpack(cfg, (err, stats) => {
    if (err) return console.error(err);
    del('release')
    .then(paths => {
      if (shouldBuildAll) {
        // build for all platforms
        const archs = ['ia32', 'x64'];
        const platforms = ['linux', 'win32', 'darwin'];

        platforms.forEach(plat => {
          archs.forEach(arch => {
            pack(plat, arch, log(plat, arch));
          });
        });
      } else {
        // build for current platform only
        pack(os.platform(), os.arch(), log(os.platform(), os.arch()));
      }
    })
    .catch(err => {
      console.error(err);
    });
  });
}

function pack(plat, arch, cb) {
  // there is no darwin ia32 electron
  if (plat === 'darwin' && arch === 'ia32') return;

  const iconObj = {
    icon: DEFAULT_OPTS.icon + (() => {
      let extension = '.png';
      if (plat === 'darwin') {
        extension = '.icns';
      } else if (plat === 'win32') {
        extension = '.ico';
      }
      return extension;
    })()
  };

  const opts = Object.assign({}, DEFAULT_OPTS, iconObj, {
    platform: plat,
    arch,
    prune: true,
    out: `release/${plat}-${arch}`
  });

  packager(opts, cb);
}


function log(plat, arch) {
  return (err, filepath) => {
    if (err) return console.error(err);
    console.log(`${plat}-${arch} finished!`);
  };
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mmahalwycommented, Feb 8, 2016

@malept i ended up using jsonfile to write json then erase it later.

0reactions
maleptcommented, Feb 8, 2016

Given an app that looks like this:

main.js
package.json

And a package.json that looks like this:

{
  "name": "MyApp",
  "main": "main.js",
  "devDependencies": {
    "electron-packager": "^5.2.1",
    "electon-prebuilt": "^0.36.0"
  }
}

I would have a main.js that starts like this:

require('frozenenv');

…and a shell script build.sh:

#!/bin/bash

npm install
echo "process.env.FOOBAR = '$FOOBAR';" > frozenenv.js
electron-packager . --out ../packages --all

What it amounts to is a different way of creating a config file. I am not aware of any other method of configuring a packaged app that involves “freezing” environment variables that doesn’t look something like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set environment variables from within package.json?
I want to set environment variables (like NODE_ENV ) in the start script while still being able to start the app with just...
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
How to set Environment Variable in node js? | Thirdock Techkno
Working with environment variables is a great way to configure different aspects of your Node.js application.
Read more >
How to set environment variables from within package json
Set the environment variable in the script command: ... "scripts": { "start": "node app. js", "test": "env NODE_ENV=test mocha --reporter spec" } ...
Read more >
Environment variables in Node.js. The Right way!
First create a file with name .env in the root of the project which contains all variable which will be injected in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found