简体   繁体   English

Node.js进程的view.env变量

[英]View .env variables of Node.js process

I use package dotenv to set env variables in .env file.我使用 package dotenv.env文件中设置环境变量。 Here is the content of .env :这是.env的内容:

NODE_ENV=development
PORT=4000

Then, I run nodemon --exec babel-node -- src/index.js to start the project.然后,我运行nodemon --exec babel-node -- src/index.js来启动项目。 How to check value of env variables by command line?如何通过命令行检查env变量的值?

I don't want to use console.log in the JS source code, I want something like so (in terminal):我不想在 JS 源代码中使用console.log ,我想要这样的东西(在终端中):

node
> process.env

But it doesn't show the values I have set in .env file.但它没有显示我在.env文件中设置的值。

Could you give me some resolution?你能给我一些解决方案吗?

From your console, type following for mac/ubuntu:在您的控制台中,为 mac/ubuntu 键入以下内容:

echo $NODE_ENV

and following for windows:对于 windows 如下:

 echo %NODE_ENV% 

If you want to avoid the use of dotenv as sometimes it creates confusion that where to call the dotenv.如果您想避免使用 dotenv,因为有时会在哪里调用 dotenv 会造成混淆。 So, you can use foreman node module and can start your project by command nf start.因此,您可以使用工头节点模块,并可以通过命令 nf start 启动您的项目。 It will automatically load.env variables into your project without calling dotenv.它会自动将 .env 变量加载到您的项目中,而无需调用 dotenv。

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

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