简体   繁体   English

在 package.json 中设置一个 env var 以用于电子重新加载?

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

I usually develop on macOS but I've moved the project over to Windows 10 in order to work on some Windows-specific issues.我通常在 macOS 上开发,但我已将项目转移到 Windows 10 以解决一些特定于 Windows 的问题。 I use electron-reload to reload the app when changes are made.当进行更改时,我使用electron-reload来重新加载应用程序。 It's been working wonderfully on macOS but breaks on Windows.它在 macOS 上运行得很好,但在 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"使用下面的设置和npm start启动应用程序,在 Windows 上它会抛出错误: "'APP__DEV' is not recognized as an internal or external command"

Am I doing this wrong and macOS is just more "forgiving"?我做错了吗,macOS 只是更“宽容”? 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.我看到了这个问题: 在 package.json 中设置 process.env var并且接受的答案看起来与我正在做的相同,所以我很困惑。

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 .如果重要的话——我在将项目移动到 Windows 时没有对 CTLF 等进行任何转换——我只是使用DropBox复制它。


package.json包.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);
}

The syntax ENV_VAR=value program arguments is a UNIX thing.语法ENV_VAR=value program arguments是 UNIX 的东西。 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). Windows 不提供在同一命令中设置环境变量和运行程序的方法,但是,这通常会起作用: set ENV_VAR=value && program arguments (因此,在您的情况下: set APP_DEV=true && electron .是什么您正在寻找)。 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.作为建议,查看dotenv和/或cross-os以使您的项目在所有系统上更可用(在这方面)而不会太头疼。

Have you tried moving your argument till after the electron command ("electron") and src location (".")?您是否尝试过将参数移动到电子命令(“electron”)和 src 位置(“.”)之后?

APP_DEV does NOT exists: APP_DEV 不存在:

"start": "APP_DEV=true electron ."

APP_DEV does exists: APP_DEV 确实存在:

"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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM