简体   繁体   中英

NODE_ENV in windows powershell returns undefined

Windows path is set to nodejs. However I cannot read the process.env.NODE_ENV unless using Command Prompt AS Admin.

I want to be able to set and read NODE_ENV in Windows powershell.

Environment variables can be set on 3 levels in Windows Machine , User , Process , which are inheriting from parent processes when the child-process starts. Of course User level ones only inherits if the process running in that user's context.

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.

With this sense PowerShell also automatically inherits env-vars, which you can see under the 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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