[英]NodeJS not recognizing .env file
I have like 5 NodeJS services running, but I have a problem in one of those.我有 5 个 NodeJS 服务正在运行,但其中一个有问题。
This is the nodemon.json
file:这是
nodemon.json
文件:
{
"watch": ["**/*.ts"],
"ext": "ts,json",
"ignore": ["./test/*.ts"],
"exec": "node -r ts-node/register -r dotenv/config Index.ts dotenv_config_path=$(pwd)/.env",
"env": {
"NODE_ENV": "development"
}
}
It's the same as the rest of services.与服务的 rest 相同。 When I run
npm run dev
I got error messages depending on which value is taking from the.env file, example:当我运行
npm run dev
时,我收到错误消息,具体取决于从 .env 文件中获取的值,例如:
const LOCAL_CONFIGURATION = {
PORT_APP: 8082,
MONGODB: {
SERVER: process.env.MONGO_DTE,
AUTH: {
auth: {
password:process.env.MONGO_PASSWORD,
user:process.env.MONGO_USER
}
},
},
MS_NOTIFICACION: "http://localhost:8089/notificacion",
ELASTIC_PATH: process.env.ELASTIC_PATH,
...COMMON,
};
The first error message is: ConfigurationError: Missing node(s) option
That message is produced because it's not reading the value from process.env.ELASTIC_PATH
, but if I put a hardcoed value like " http://with.the.correct.url " and it tries again to run, I get another error:第一条错误消息是:
ConfigurationError: Missing node(s) option
产生该消息是因为它没有从process.env.ELASTIC_PATH
读取值,但是如果我输入了一个硬编码值,例如“ http://with.the.correct. url ”,它再次尝试运行,我得到另一个错误:
Error: Credentials must be provided when creating a service client
That error is because it's trying to read password:process.env.MONGO_PASSWORD
and user:process.env.MONGO_USER
Error: Credentials must be provided when creating a service client
该错误是因为它正在尝试读取password:process.env.MONGO_PASSWORD
和user:process.env.MONGO_USER
etc, so, there's a problem on reading the .env
file.等等,因此,读取
.env
文件时出现问题。 I know that .env
file has those values, and the file is in UTF-8, without quotes, etc. The .env
file is the same file as the other services, it works ok in the rest but I don't know why is not getting read here.我知道
.env
文件具有这些值,并且该文件位于 UTF-8 中,没有引号等。 .env
文件与其他服务是相同的文件,它在 rest 中可以正常工作,但我不知道为什么不是在这里阅读。
Any idea?任何想法?
EDIT:编辑:
Plus, I put a console.log(process.env);
另外,我放了一个
console.log(process.env);
in config.ts
file and it shows values like this:在
config.ts
文件中,它显示如下值:
But there's no values from the .env
for example, there in the picture there's a value called COMPUTERNAME
so if I put console.log(process.env.COMPUTERNAME);
但是没有来自
.env
的值,例如,图片中有一个名为COMPUTERNAME
的值,所以如果我输入console.log(process.env.COMPUTERNAME);
I get: IBM-NOT87
我得到:
IBM-NOT87
Why is not getting the.env file?为什么没有得到 .env 文件?
require('dotenv').config({ path: "./sample.env" });
In the file you are using environment variables, As early as possible, require the "dotenv"
and in the config()
method, specify the path of the .env
file, even if it in your root directory or the same directory where node starts.在您使用环境变量的文件中,尽可能早地要求
"dotenv"
,并在config()
方法中指定.env
文件的路径,即使它在您的根目录或节点启动的同一目录中.
The code for requiring and specifying file in the same directory is in the first line in the answer.要求和指定同一目录中的文件的代码位于答案的第一行。
Also, for further reading, you can visit https://github.com/motdotla/dotenv#path另外,为了进一步阅读,您可以访问https://github.com/motdotla/dotenv#path
you should use this on your app.js
你应该在你的
app.js
上使用它
Sharing image, as its sometimes easier to see expanded分享图片,因为它有时更容易看到 展开
code here =>代码在这里=>
require('dotenv/config') // require the dotenv/config at beginning of file
const express = require('express')
const mongoose = require('mongoose')
You cat try this.你猫试试这个。
-> npm i dotenv -> npm 我 dotenv
and in code add this piece of code并在代码中添加这段代码
require('dotenv').config({
path: 'your path here'
})
Install dotenv package安装dotenv package
npm install --s dotenv
npm 安装 --s dotenv
And add this require("dotenv").config();
并添加此
require("dotenv").config();
in index.js/ts file.在 index.js/ts 文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.