如何在 node.js 中设置环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10829433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set an environmental variable in node.js?
提问by abrkn
How can I set an environmental variable in node.js?
如何在 node.js 中设置环境变量?
I would prefer not to rely on anything platform specific, such as running export or cmd.exe's set.
我不想依赖任何特定于平台的东西,例如运行 export 或 cmd.exe 的设置。
回答by lanzz
You can set your environment variables in process.env:
您可以在process.env以下位置设置环境变量:
process.env['VARIABLE'] = 'value';
-OR-
-或者-
process.env.VARIABLE = 'value';
Node should take care of the platform specifics.
Node 应该处理平台的细节。
回答by jagjeet
First you should install this package :-
https://github.com/motdotla/dotenv[npm install dotenv]
首先你应该安装这个包:-
https://github.com/motdotla/dotenv[ npm install dotenv]
Then you need to create a .env file in your project's root directory, and there you can add variables like below:-
然后你需要在你的项目的根目录中创建一个 .env 文件,你可以在那里添加如下变量:-
NODE_ENV=PRODUCTION
DATABASE_HOST=localhost
Now you can easily access these variables in your code like below:-
现在,您可以在代码中轻松访问这些变量,如下所示:-
require('dotenv').config()
console.log(process.env.NODE_ENV);
It worked for me, hopefully that helps.
它对我有用,希望能有所帮助。
回答by Lord
node v14.2.0To set env variable first create a file name config.env in your project home directory and then write all the variables you need, for example
node v14.2.0要设置 env 变量,首先在你的项目主目录中创建一个文件名 config.env ,然后写入你需要的所有变量,例如
config.env
配置文件
NODE_ENV=development
PORT=3000
DATABASE=mongodb+srv://lord:<PASSWORD>@cluster0-eeev8.mongodb.net/tour-guide?retryWrites=true&w=majority
DATABASE_LOCAL=mongodb://localhost:27017/tours-test
DATABASE_PASSWORD=UDJUKXJSSJPWMxw
now install dotenv from npm, dotenv will offload your work
现在从 npm 安装 dotenv,dotenv 将卸载你的工作
npm i dotenv
now in your server starter script, in my case it is server.js use doenv to load env variables.
现在在您的服务器启动脚本中,在我的情况下是 server.js 使用 doenv 加载环境变量。
const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const app = require('./app'); // must be after loading env vars using dotenv
//starting server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`app running on port ${port}...`);
});
I am using express, all my express code in app.js, writing here for your reference
我用的是express,我所有的express代码都在app.js中,写在这里供大家参考
const express = require('express');
const tourRouter = require('./route/tourRouter');
const userRouter = require('./route/userRouter');
if (process.env.NODE_ENV === 'development') {
console.log('mode development');
}
app.use(express.json());
app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);
module.exports = app;
now start your server using the console, I am using nodemon, you can install it from npm;
现在使用控制台启动你的服务器,我使用的是nodemon,你可以从 npm 安装它;
nodemon server.js

