这个表达如何工作? “ require(dotenv”)。

发布于 2025-02-01 16:10:17 字数 326 浏览 3 评论 0原文

我看到了此表达式:

require('dotenv').config();

在nodejs项目中server.js文件的开头。我只是想知道它是如何工作的,它做什么?

因为几乎总是我在需要表达行之类的

 const express = require('express'); 

之后

const app = express();

都看到了一个变量。而且它没有像通用方法那样使用。

I saw this expression:

require('dotenv').config();

At the beginning of a server.js file within a NodeJS project. I am just curious to know how does it work and what does it do?

Because almost always I have seen a variable before the require expression line like

 const express = require('express'); 

and afterwards it will be used somehow like

const app = express();

But require('dotenv').config(); looks different and it hasn't been used like the common approach.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花期渐远 2025-02-08 16:10:17

导入类型

当您在package.json文件中定义type时,它将为整个项目设置该导入类型。 require()不能在类型模块import中使用在类型commonjs

中使用

require()不能在类型模块import中使用 在类型commonjs 示例包 .json

{
  "name": "stack",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "type": /* either commonJS or module */
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

设置导入类型后,这是两种类型的

// if "type": "commonJS"
const package = require('package')

// if "type": "module"
import package from 'package'

“ dotenv”

中这两种类型的示例,这是读取.env文件的环境变量模块。 方法

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

import {} from 'dotenv/config' // module

是导入它

以下

TOKEN=THIS_IS_MY_TOKEN

的两种 .js file'express

/* after importing 'dotenv' one of the two ways listed above */

console.log(process.env.TOKEN) // outputs "THIS_IS_MY_TOKEN"

'

的两种方法。

const express = require('express') // commonJS

import express from 'express' // module

const app = express() // initializes express app

这是用commonjsmodule

es模块是JavaScript的标准,而commonjs是节点中的默认值。 JS。创建ES模块格式是为了标准化JavaScript模块系统。它已成为封装JavaScript代码以重复使用的标准格式。

Import Types

When you define a type in the package.json file, it will set that import type for the whole project. require() cannot be used in type module and import cannot be used in type commonJS

Example package.json

{
  "name": "stack",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "type": /* either commonJS or module */
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

after setting the import type here are the examples of both types in action

// if "type": "commonJS"
const package = require('package')

// if "type": "module"
import package from 'package'

'dotenv'

This is the environment variables module to read .env files. Below are the two ways to import it whether you're using type commonJS or module

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

import {} from 'dotenv/config' // module

works like this...

.env file

TOKEN=THIS_IS_MY_TOKEN

then in a .js file

/* after importing 'dotenv' one of the two ways listed above */

console.log(process.env.TOKEN) // outputs "THIS_IS_MY_TOKEN"

'express'

this is the two ways to import express with either commonJS or module

const express = require('express') // commonJS

import express from 'express' // module

const app = express() // initializes express app

CommonJS vs ES Module

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文