在package.json中设置env var以与电子重装一起使用? [英] Setting an env var in package.json for use with electron-reload?
问题描述
我通常在macOS上进行开发,但是为了解决某些特定于Windows的问题,我将该项目移至Windows 10。进行更改后,我使用 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. I use electron-reload
to reload the app when changes are made. It's been working wonderfully on macOS but breaks on Windows.
使用下面的设置,然后 npm start
来启动macOS。应用程序,在Windows上会引发错误:'APP__DEV'不被识别为内部或外部命令
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"
我做错了吗,macOS更宽容了?我看到了这个问题:在package.json中设置process.env var 并且接受的答案与我正在执行的操作相同,因此感到困惑。
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.
如果有关系-将项目移至Windows时我没有进行CTLF等任何转换-我只是使用 DropBox
复制了它。
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
package.json
"start": "APP_DEV=true electron .",
Main.js
Main.js
let isDev = process.env.APP_DEV ? (process.env.APP_DEV.trim() == "true") : false;
if (isDev) {
require('electron-reload')(__dirname);
}
推荐答案
语法 ENV_VAR =值程序参数
是UNIX。 Windows没有提供一种设置环境变量并在同一命令中运行程序的方法,但是,这通常会起作用: set ENV_VAR = value&&程序参数
(因此,在您的情况下: set APP_DEV = true&& electronic。
是您所要的)。作为建议,请查看 dotenv 和/或跨操作系统,使您的项目在所有系统上都可以使用(就此而言),而不会造成太多麻烦。
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.
这篇关于在package.json中设置env var以与电子重装一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!