node.jsvisual-studio-codeterminalenvironment-variablesdevelopment-environment

Node.js does not read env variable set in Mac native Terminal


I am running Node.js application from VS Code. I wanted to save my Database password in an env variable. I exported the data on my Mac native Terminal app and on echo, I was able to retrieve the data.

export MONGO_PASSWORD="PASSWORD"
echo $MONGO_PASSWORD // prints the password

However, when I open the integrated terminal in VS Code, it does not show the value.

echo $MONGO_PASSWORD // prints blank

Also, when I connected to node CLI and typed process.env, it did not include my variable.

process.env.MONGO_PASSWORD // prints undefined

As a workaround, I was able to set and export the variable from the ~/.zshrc file on my Mac. However, I am still curious why the VS Code integrated terminal isn't able to read values exported within the Mac Terminal and if there is a way to fix that.


Solution

  • I am still curious why the VS Code integrated terminal isn't able to read values exported within the Mac Terminal and if there is a way to fix that.

    In zsh, export does exactly what it says it does in the associated man page:

    The specified names are marked for automatic export to the environment of subsequently executed commands […]

    Exports in the integrated terminal in VSCode are performed in a separate instance of the shell environment that you’ve spawned in Terminal.app, so one would not be aware of names exported within the other.

    There isn’t a way to "fix" this behavior as it’s operating as designed/intended.