Search code examples
environment-variableswindows-terminalnpm-config

Cannot set environment variables for npm-config in PowerShell


When I am trying to set environment variables using PowerShell in Windows Terminal with the command set test1=value1, I get no errors. However, when I try to check all environment variables using the set command, I get the following prompt:

cmdlet Set-Variable at command pipeline position 1
Supply values for the following parameters:
Name[0]:

I read that when using PowerShell you set environment vars using this:

$Env:test1 = "value1";    

I want to set the variables so that on my backend in custom-environment-variables.json I can store a name by which config can extract it using config.get("test").

custom-environment-variables.json:

{
    "test": "test1",
}

But every time I try this, it says Error: Configuration property "test" is not defined.

Doing the same procedure CMD (either directly or through Windows Terminal) I get no issues whatsoever. Any ideas what might be causing this?


Solution

  • First, the easy part:

    I get no errors but when I try to check all env. variables calling "set" I get the following prompt:

    That's because the set command in PowerShell behaves differently. It's an alias for the PowerShell Set-Variable cmdlet. You can see this with Get-Alias.

    Also, PowerShell variables are not environment variables. As you commented, the proper way to set an environment variable in PowerShell is with:

    $env:variablename = "value"
    

    The equivalent command to set (to get a list of all environment variables and their values) in PowerShell is:

    Get-ChildItem env:
    # Or using the alias
    dir env:
    # Or using another alias
    ls env:
    

    This access the PowerShell "environment provider", which is essentially (my grossly oversimplified summary) a "virtual drive/filesystem" that PowerShell provides which contains the environment variables. You can also create variables in here.

    More reading: about_Environment_Variables from the PowerShell Doc.


    As for the core issue with the config module, I haven't been able to reproduce that. It works correctly for me in both PowerShell and CMD. So let me run through my results in the hopes that it will help you see what might be different. All tests were performed in Windows Terminal, although as we've determined in the comments, this is more a difference in PowerShell vs. CMD for you:

    config\default.json:

    {
      "test": "Original Value"
    }
    

    config\custom-environment-variables.json:

    {
      "test": "test1"
    }
    

    CMD without test1 variable set:

    Running node in CMD:

    > const config = require('config')
    undefined
    > config.get('test')
    'Original Value'
    >
    

    CMD with test1 variable set:

    Exit Node, and back in CMD:

    >set test1=Override
    >node
    

    In Node:

    Welcome to Node.js v14.16.1.
    Type ".help" for more information.
    > const config = require('config')
    undefined
    > config.get('test')
    'Override'
    >
    

    PowerShell without test1 variable set:

    Welcome to Node.js v14.16.1.
    Type ".help" for more information.
    > const config = require('config')
    undefined
    > config.get('test')
    'Original Value'
    >
    

    PowerShell with test1 variable set:

    In PowerShell:

    PS1> $env:test1="Override"
    PS1> node
    

    In Node:

    Welcome to Node.js v14.16.1.
    Type ".help" for more information.
    > const config = require('config')
    undefined
    > config.get('test')
    'Override'
    >