当我试图用命令PowerShell在窗口终端中使用set test1=value1设置环境变量时,不会出现错误。但是,当我尝试使用set命令检查所有环境变量时,我会得到以下提示:
cmdlet Set-Variable at command pipeline position 1
Supply values for the following parameters:
Name[0]:我看到在使用PowerShell时,使用以下方法设置环境vars:
$Env:test1 = "value1"; 我希望设置变量,以便在custom-environment-variables.json的后端存储一个名称,配置可以通过这个名称使用config.get("test")提取它。
custom-environment-variables.json
{
"test": "test1",
}但是每次我尝试这个,它都说Error: Configuration property "test" is not defined。
做同样的过程,CMD (直接或通过Windows终端)我没有任何问题。有什么原因吗?
发布于 2022-02-23 21:36:02
首先,简单的部分:
我没有错误,但是当我试图检查所有的env时。调用"set“的变量得到以下提示:
这是因为set命令在PowerShell中的行为不同。它是PowerShell Set-Variable cmdlet的别名。您可以在Get-Alias中看到这一点。
而且,PowerShell变量不是环境变量。正如您所评论的,在PowerShell中设置环境变量的正确方法是:
$env:variablename = "value"set (获取PowerShell中所有环境变量及其值的列表)的等效命令是:
Get-ChildItem env:
# Or using the alias
dir env:
# Or using another alias
ls env:这个访问PowerShell“环境提供者”,本质上(我过于简化的总结)是PowerShell提供的一个“虚拟驱动器/文件系统”,其中包含了环境变量。您也可以在这里创建变量。
更多阅读:变量来自PowerShell医生。
至于config模块的核心问题,我还无法重现。在PowerShell和CMD中,它对我都是正确的。所以,让我把我的结果做一遍,希望它能帮助你了解什么是不同的。所有的测试都是在Windows终端上执行的,尽管正如我们在注释中所确定的那样,这在PowerShell和CMD方面有更大的区别:
config\default.json
{
"test": "Original Value"
}config\custom-environment-variables.json
{
"test": "test1"
}没有test1变量集的CMD:
在CMD中运行node:
> const config = require('config')
undefined
> config.get('test')
'Original Value'
>具有test1变量集的CMD:
退出节点,返回CMD:
>set test1=Override
>node在Node:
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Override'
>没有PowerShell变量集的test1:
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Original Value'
>具有PowerShell变量集的test1:
在PowerShell中:
PS1> $env:test1="Override"
PS1> node在Node:
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Override'
>https://stackoverflow.com/questions/71242653
复制相似问题