Electron-builder: How to set an environment variable before starting packaged electron

Created on 19 Jan 2017  路  6Comments  路  Source: electron-userland/electron-builder

  • 10.7.0:

  • 1.4.14: WIndows 64bit


This is not an issue, more a plea for help.

I have an application that needs more than the default thread count (4) to handle multiple serialports. I can controlled by setting UV_THREADPOOL_COUNT in package.json and everything works great.

"start": "cross-env UV_THREADPOOL_SIZE=20 electron ."

running on Windows 7 / 10 / mac. However my packaged application does not have the variable set.
I've attempted to set this using process.env.UV_THREADPOOL_SIZE in main.js but it looks like its too late at this point. Other attempts to either require in the app or set in the build script via --js-flags is failing.

Has anyone had this or a similar environment issue they have managed to package?

2 days of Googling and I've not managed a working solution other than npm start which is not a
viable for a customer to do.

question windows

All 6 comments

http://stackoverflow.com/a/3036367/1910191

Create .cmd file and run it instead of app exe directly.

You need to change https://github.com/electron-userland/electron-builder/blob/master/packages/electron-builder/templates/nsis/installSection.nsh#L139

https://github.com/electron-userland/electron-builder/wiki/NSIS#custom-nsis-script (yes, not easy to change the only line, maybe you can fork for now and later send a PR).

Many thanks. I'll give that a go.

Hi, ferrisimo
I also have the same issue, how to solve it finally?
Thanks!

@ferrisimo could you share how you fixed it please? :)

I ended up just setting the variable in Windows I think as a manual step. Will temind myself when I鈥檓 back in the office. Apologies for the slow response!

I ended up using node-powershell to set the variable. Here's my code, if anyone is interested:

        if (process.platform === 'win32') {
          const Shell = require('node-powershell')
          let ps = new Shell({
            executionPolicy: 'Bypass',
            noProfile: true
          })
          const command = "[Environment]::SetEnvironmentVariable('UV_THREADPOOL_SIZE', 128, 'User')"
          ps.addCommand(command)
          ps.invoke()
            .then(output => {
              dialog.showMessageBox({
                type: 'info',
                message: 'First time setup done.'
              })
              app.relaunch()
              app.quit()
            })
            .catch(err => {
              console.log(err)
              ps.dispose()
            })
        }
Was this page helpful?
0 / 5 - 0 ratings