Setting an env var in package.json for use with electron-reload?

别来无恙 提交于 2020-07-21 07:40:07

问题


I usually develop on macOS but I've moved the project over to Windows 10 in order to work on some Windows-specific issues. I use electron-reload to reload the app when changes are made. It's been working wonderfully on macOS but breaks on Windows.

Using the setup below, and npm start to start the app, on Windows it throws an error: "'APP__DEV' is not recognized as an internal or external command"

Am I doing this wrong and macOS is just more "forgiving"? I saw this question: Setting process.env var in package.json and the accepted answer looks the same as what I am doing so I'm confused.

Before I jump down the rabbit hole, I thought I would ask if there is something simple wrong with what I am doing.

If it matters – I didn't do any conversion of CTLF, etc when moving the project to Windows – I just copied it over using DropBox.


package.json

"start": "APP_DEV=true electron .",

Main.js

let isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == "true") : false;

if (isDev) {
    require('electron-reload')(__dirname);
}

回答1:


The syntax ENV_VAR=value program arguments is a UNIX thing. Windows does not provide a way to set an environment variable and run a program in the same command, however, this will generally work: set ENV_VAR=value && program arguments (so, in your case: set APP_DEV=true && electron . is what you're looking for). As a suggestion, look at dotenv and/or cross-os to make your project more usable (in this regard) on all systems without too much headache.




回答2:


Have you tried moving your argument till after the electron command ("electron") and src location (".")?

APP_DEV does NOT exists:

"start": "APP_DEV=true electron ."

APP_DEV does exists:

"start": "electron . APP_DEV=true"

EDIT:

The above mentioned method would not be able to retrieve as enviromental variables, but as process arguments. Not sure if this will be able to solve your issue.

string[] argument = process.argv;


来源:https://stackoverflow.com/questions/60659194/setting-an-env-var-in-package-json-for-use-with-electron-reload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!