【问题标题】:What does require('dotenv').config() do?require('dotenv').config() 有什么作用?
【发布时间】:2021-01-23 00:17:13
【问题描述】:

我知道dotenv模块与nodejs中的环境变量有关

require("dotenv").config();

我知道我应该把这段代码放在我的 nodejs 服务器文件中。 但问题是我不明白 config 方法实际上在做什么?

    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;

我查看了配置函数,但我无法理解这段代码的作用?

【问题讨论】:

    标签: javascript node.js dotenv


    【解决方案1】:

    您正在查看类型声明文件,但您应该查看编写为 here 的实际 config 实现。

    但是,config 方法将 .env 文件路径作为参数,它对其进行解析并在 process.env 中设置该文件中定义的环境变量。

    类型声明文件是 Typescript 概念,它允许您为已编写的 Javascript 代码设置具体的变量/参数类型。

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 2020-06-14
      • 2020-01-31
      • 2020-10-14
      • 2017-05-23
      • 2023-03-03
      • 2021-07-08
      • 1970-01-01
      相关资源
      最近更新 更多