简体   繁体   English

require('dotenv').config() 有什么作用?

[英]What does require('dotenv').config() do?

I know dotenv module is related to environmental variables in nodejs我知道 dotenv 模块与 nodejs 中的环境变量有关

require("dotenv").config();

and I know that I should put this code in my nodejs server file.我知道我应该把这段代码放在我的 nodejs 服务器文件中。 but the problem is I didn't understand what config method is actually doing?但问题是我不明白配置方法实际上在做什么?

    export interface DotenvConfigOptions {

  path?: string;

  encoding?: string;

  debug?: boolean;
}

export interface DotenvConfigOutput {
  error?: Error;
  parsed?: DotenvParseOutput;
}

/**
 * Loads `.env` file contents into {@link https://nodejs.org/api/process.html#process_process_env | `process.env`}.
 * Example: 'KEY=value' becomes { parsed: { KEY: 'value' } }
 *
 * @param options - controls behavior
 * @returns an object with a `parsed` key if successful or `error` key if an error occurred
 *
 */
export function config(options?: DotenvConfigOptions): DotenvConfigOutput;
/** @deprecated since v7.0.0 Use config instead. */
export const load: typeof config;

I looked into config function but I couldn't understand what this code does?我查看了配置 function 但我不明白这段代码的作用?

You are looking at the types declaration file, but you should look at the actual config implementation which is written here .您正在查看类型声明文件,但您应该查看此处编写的实际config实现。

However, the config method takes a .env file path as an argument, it parses it and sets environment vars defined in that file in process.env .但是, config方法将.env文件路径作为参数,它对其进行解析并在process.env中设置该文件中定义的环境变量。

A type declaration file is Typescript concept which allows you to set concrete variable/parameter types for already written Javascript code.类型声明文件是 Typescript 概念,它允许您为已编写的 Javascript 代码设置具体的变量/参数类型。

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

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