简体   繁体   English

无法在 Visual Studio Code 中更改 node.js 中的环境变量

[英]Unable to change environmental variables in node.js in Visual Studio Code

I'm new here and I'm new with this programming language, so maybe my question is something very obvious.我是新来的,我是这种编程语言的新手,所以也许我的问题很明显。

I am trying to set an environmental variable in Node.js in Visual Studio Code (for example NODE_ENV to production).我试图在 Visual Studio Code 的 Node.js 中设置一个环境变量(例如 NODE_ENV 到生产)。 I've tried setting it using an .env file and using the dotenv package.我尝试使用 .env 文件和 dotenv 包设置它。 This does not seem to work, and when I try to print the variable to the console it prints 'undefined'.这似乎不起作用,当我尝试将变量打印到控制台时,它会打印“未定义”。 This is the part that I have included in the .env file:这是我包含在 .env 文件中的部分:

NODE_ENV = production 

This is a part of the js file that I try to run:这是我尝试运行的 js 文件的一部分:

 const path = require('path') require('dotenv').config() console.log(process.env.NODE_ENV)

When I run this 'undefined' gets printed to the console.当我运行这个“未定义”时,它会打印到控制台。 I also tried passing the place of the file as an argument with the config for dotenv, like this .我还尝试将文件的位置作为参数与 dotenv 的配置一起传递,如下所示 That did not work.那没有用。 I even tried to set te environmental variables directly via launch.json like this:我什至尝试通过 launch.json 直接设置环境变量,如下所示:

 "version": "0.2.0", "configurations": [ { "type": "pwa-node", "request": "launch", "name": "Launch Program", "skipFiles": [ "<node_internals>/**" ], "program": "${file}", "env": { "NODE_ENV" : "production", } } ] }

That also did not work.那也没有用。

Why is this?为什么是这样? Why isn't Visual Studio Code able to change the environmental variables?为什么 Visual Studio Code 不能更改环境变量? Thanks in advance.提前致谢。

According to the docs of dotenv there should be no space between the variable name and its value.根据dotenv的文档,变量名与其值之间不应有空格。

The syntax is: VARNAME=VALUE语法是: VARNAME=VALUE

So your .env file should be所以你的.env文件应该是

NODE_ENV=production

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

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