简体   繁体   English

Windows Powershell中的NODE_ENV返回未定义

[英]NODE_ENV in windows powershell returns undefined

Windows path is set to nodejs. Windows路径设置为nodejs。 However I cannot read the process.env.NODE_ENV unless using Command Prompt AS Admin. 但是, 除非使用Command Prompt AS Admin, 否则我无法阅读process.env.NODE_ENV。

I want to be able to set and read NODE_ENV in Windows powershell. 我希望能够在Windows powershell中设置和读取NODE_ENV。

Environment variables can be set on 3 levels in Windows Machine , User , Process , which are inheriting from parent processes when the child-process starts. 可以在Windows MachineUserProcess 3个级别上设置环境变量,它们在子进程启动时从父进程继承。 Of course User level ones only inherits if the process running in that user's context. 当然,仅当进程在该用户的上下文中运行时,才会继承User级别的进程。

On the other hand, if the parent process (eg: System ) changes a variable, these changes are not propagated automatically to already running child-processes (eg: PowerShell , or node ), unless you restart these. 另一方面,如果父进程(例如: System )更改了变量,则这些更改不会自动传播到已经运行的子进程(例如: PowerShellnode ),除非您重新启动它们。

With this sense PowerShell also automatically inherits env-vars, which you can see under the env: PSDrive: 从这个意义上说,PowerShell还自动继承了env-vars,您可以在env:下看到它env: PSDrive:

ls env:\*

If you can't see the new variable and you would like to avoid a restart, you can also use [System.Environment] .NET class to read and populate this in an already running console, eg: 如果看不到新变量,并且希望避免重新启动,则还可以使用[System.Environment] .NET类在已经运行的控制台中读取并填充它,例如:

$env:NODE_ENV = [System.Environment]::GetEnvironmentVariable('NODE_ENV',[System.EnvironmentVariableTarget]::Machine)

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

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