简体   繁体   English

服务器 nodejs 无法识别 dotenv 文件

[英]dotenv file is not recognized by server nodejs

this is my first mern stack project ever and i'm trying to push it to github.这是我的第一个 mern 堆栈项目,我正在尝试将其推送到 github。 i am using mongoose so i had to hide the login and pwd of my account i looked it up and found the.env solution.我正在使用 mongoose 所以我不得不隐藏我的帐户的登录名和密码,我查了一下并找到了.env 解决方案。 I created a dot env file我创建了一个点 env 文件

Port=5000 CONNECTION_URL=mongodb+srv://+++++++++@cluster0.p5v9c.mongodb.net/++++++?retryWrites=true&w=majority端口=5000 CONNECTION_URL=mongodb+srv://+++++++++@cluster0.p5v9c.mongodb.net/++++++?retryWrites=true&w=majority

i ran npm i dot env我跑了 npm 我点 env

then i added import statement:然后我添加了导入语句:

import dotenv from 'dotenv';从“dotenv”导入 dotenv;

and the problem seems to occur here, i tried:问题似乎发生在这里,我试过:

dotenv.config() require('dotenv').config(); dotenv.config() require('dotenv').config(); require ('dotenv/config');

the code to of my index js if it helps to solve the solution:如果它有助于解决解决方案,我的索引 js 的代码:

const PORT = process.env.PORT|| 5000;

mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => app.listen(PORT, () => console.log(`Server Running on Port: http://localhost:${PORT}`)))
  .catch((error) => console.log(`${error} did not connect`));

mongoose.set('useFindAndModify', false);

can anyone help me solve this up i would really appreciate it thank you ^^谁能帮我解决这个问题,我真的很感激谢谢你^^

After installing you do not need to import dotenv.安装后不需要导入 dotenv。 Remove the line:删除行:

import dotenv from 'dotenv';从“dotenv”导入 dotenv;

Include dotenv with just:仅包含 dotenv:

require('dotenv').config()需要('dotenv').config()

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

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