简体   繁体   English

在 node.js 中使用 .env 文件后 process.env 未定义

[英]process.env is undefined after using .env file in node.js

I have created an application using node.js & protractor.我使用 node.js 和 protractor 创建了一个应用程序。 the following start script is used in the package.json file for running my application以下启动脚本在 package.json 文件中用于运行我的应用程序

"start": "protractor conf/conf.js"

I have created a.env file and add one sample variable TestManaf='1234556' But it shows error like 'the value is undefined'.我创建了一个 .env 文件并添加了一个示例变量TestManaf='1234556'但它显示错误,例如“值未定义”。

After this, I have directly added TestManaf='1234556' in the start script.在此之后,我在启动脚本中直接添加了TestManaf='1234556'

"start": "set TestManaf='1234556' && protractor conf/conf.js"

It is working fine.它工作正常。 Why.env file not supported?为什么.env 文件不支持? is any additional configuration required?是否需要任何额外的配置?

You need to install the package dotenv您需要安装 package dotenv

use the following command:使用以下命令:

npm install dotenv

First thing you should do in your file is load the dotenv env vars, put this line as soon as possible in your import hierarchy.您应该在文件中做的第一件事是加载 dotenv 环境变量,尽快将此行放入导入层次结构中。

require('dotenv').config()

You can see more information about using this package in the documentation您可以在文档中查看有关使用此 package 的更多信息

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

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