首先,简单的部分:
我没有收到任何错误,但是当我尝试检查所有环境时。调用“set”的变量我得到以下提示:
这是因为set
PowerShell 中的命令行为不同。Set-Variable
它是 PowerShell 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 Doc 中的about_Environment_Variables。
至于config
模块的核心问题,我无法重现。它在 PowerShell 和 CMD 中都能正常工作。因此,让我回顾一下我的结果,希望它能帮助您了解可能会有所不同。所有测试都是在 Windows 终端中执行的,尽管正如我们在评论中确定的那样,这对您来说是 PowerShell 与 CMD 的更多区别:
config\default.json
:
{
"test": "Original Value"
}
config\custom-environment-variables.json
:
{
"test": "test1"
}
test1
没有变量集的 CMD :
node
在 CMD 中运行:
> const config = require('config')
undefined
> config.get('test')
'Original Value'
>
test1
带有变量集的 CMD :
退出节点,然后返回 CMD:
>set test1=Override
>node
在节点中:
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Override'
>
test1
没有变量集的 PowerShell :
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Original Value'
>
test1
带有变量集的 PowerShell :
在 PowerShell 中:
PS1> $env:test1="Override"
PS1> node
在节点中:
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> const config = require('config')
undefined
> config.get('test')
'Override'
>