NodeJS环境变量未定义 [英] NodeJS environment variables undefined
本文介绍了NodeJS环境变量未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一些环境变量,但是当我创建文件并运行服务器时,似乎未定义.我正在使用nodemon
.我已经重新启动服务器,但是没有运气.
I'm trying to create some envrioment variables but when I create the file and run the server the seem to be undefined. I'm using nodemon
. I have restarted my server and no luck.
已更新
.env
MONGO_ATLAS_PW = "xxxx";
JWT_KEY = "secret_this_should_be_longer";
package.json
...
"scripts": {
...
"start:server": "nodemon ./server/server.js"
}
app.js
require('dotenv').config();
...
console.log(process.env.JWT_KEY); //undefined
推荐答案
此文件必须位于我项目的根目录中.
This needed to be in the root directory of my project.
nodemon.json
{
"env": {
"MONGO_ATLAS_PW": "xxxx",
"JWT_KEY": "secret_this_should_be_longer"
}
}
这篇关于NodeJS环境变量未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文