nodejs导出导入_NodeJS导出和导入模块

本文介绍了NodeJS中如何导出和导入模块,强调了模块化的重要性,特别是对于可重用性和避免重复开发。文章详细讲解了如何导出变量、函数和整个模块,并通过示例展示了`exports`对象的使用。同时,还阐述了如何使用`require()`函数导入模块,将模块缓存到应用中以便复用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nodejs导出导入

In my previous posts, we have discussed about “How to install Enide Studio 2014 IDE” and also “How to create a Node JS Application“.

在我以前的文章中,我们讨论了“ 如何安装Enide Studio 2014 IDE ”以及“ 如何创建Node JS应用程序 ”。

Before discussing about “How to create new Node JS Modules and How to reuse them in other Node JS Modules”, first of all we need to have some good knowledge about how to export and import a Node JS module.

在讨论“如何创建新的Node JS模块以及如何在其他Node JS模块中重用它们”之前,我们首先需要对如何导出导入 Node JS模块有一定的了解。

In this post, we are going to discuss the following two important Node JS concepts theoretically.

在本文中,我们将从理论上讨论以下两个重要的Node JS概念。

  • How to export a Node JS Module

    如何导出Node JS模块
  • How to import a Node JS Module

    如何导入Node JS模块

We will use this knowledge especially in next post, but also in all my coming post’s examples.

我们将在以后的文章中特别是在以后的例子中使用这些知识。

导出节点JS模块 (Export a Node JS Module)

First and foremost thing we need to understand is why we need to export a Node JS Module?

我们首先要了解的是为什么我们需要导出Node JS模块?

Node JS has provided almost all required modules (We can check this updates on its official website: https://www.npmjs.com/. It is also known as Node JS Module Repository). But in some realtime applications, we may have some application functionality, which is used many places in that application but not available at Node JS Module Repository.

Node JS提供了几乎所有必需的模块(我们可以在其官方网站上查看此更新: https : //www.npmjs.com/ ,也称为Node JS模块存储库)。 但是在某些实时应用程序中,我们可能具有某些应用程序功能,该功能在该应用程序中的许多地方都已使用,但在Node JS Module Repository中不可用。

In this scenario, to get Reusability benefit, we need to create our own new Node JS module. Just creating new Module is not enough to use it in other modules of the system. We need to export this module so that other modules will reuse it.

在这种情况下,为了获得可重用性的好处,我们需要创建自己的新Node JS模块。 仅创建新模块不足以在系统的其他模块中使用它。 我们需要导出此模块,以便其他模块可以重用它。

If you are a UI or Java Developer, then is familiar to you. If we have any common or reusable component in Java-based application, then we develop it as a separated project, create a Jar file and add it at required projects classpath.

如果您是UI或Java开发人员,那么您会很熟悉。 如果我们在基于Java的应用程序中具有任何公共或可重用的组件,则我们将其开发为一个单独的项目,创建一个Jar文件,并将其添加到所需的项目类路径中。

Don’t worry too much about how to create a Node JS Module at this point. We will discuss how to create our own new Node JS Module in future post.

此时,不必太担心如何创建Node JS模块。 我们将在以后的文章中讨论如何创建自己的新Node JS模块。

Node JS Platform has provided a technique to export the following things so that other modules can reuse them without redefining.

Node JS平台提供了一种导出以下内容的技术,以便其他模块可以重新使用它们而无需重新定义。

  • Variable

    变量
  • Function

    功能
  • Module

    模组

To export all these three, we have to use same technique. Node JS has provided an Object “exports” to do this.

要导出所有这三个,我们必须使用相同的技术。 Node JS提供了一个对象“导出”来执行此操作。

Syntax:

句法:

We will see one by one now with some examples. Here we are discussing them theoretically only. We will take one realtime scenario and implement one Node JS Application in my next post.

现在,我们将通过一些示例逐一查看。 在这里,我们仅在理论上讨论它们。 在下一篇文章中,我们将采用一个实时场景并实现一个Node JS应用程序。

How to export a JavaScript Variable

如何导出JavaScript变量

We use Node JS “exports” object to export a Node JS Module’s Variable so that other Modules can reuse it. In realtime, we don’t just export a Variable. However just for practice purpose and also to make it clear to beginners, we are discussing this separately.

我们使用Node JS “导出”对象导出Node JS模块的变量,以便其他模块可以重用它。 实时地,我们不只是导出变量。 但是,仅出于练习目的,并且也为了使初学者更清楚,我们正在单独讨论此问题。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: pi.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:pi.js文件,其中包含以下内容。

var PI = 3.1416
exports.PI = PI;

Here we have exported PI variable as exports.PI with name PI. That’s means other Node JS project can use this variable very easily just using PI name.

在这里,我们将PI变量导出为ex​​ports.PI,名称为PI。 这意味着其他Node JS项目仅使用PI名称就可以非常轻松地使用此变量。

How to export a JavaScript Function:

如何导出JavaScript函数:

We use Node JS same “exports” object even to export a Node JS Module’s JavaScript function so that other Modules can reuse it. In realtime, we may do this but it is not recommended to use always.

我们甚至使用Node JS相同的“导出”对象来导出Node JS模块JavaScript函数,以便其他模块可以重复使用它。 我们可以实时执行此操作,但不建议始终使用。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:arthmetic.js文件,其内容如下。

function add(a,b){
return a + b;
}

function sub(a,b){
return a - b;
}

function mul(a,b){
return a * b;
}

function div(a,b){
return a / b;
}

exports.add = add
exports.sub = sub
exports.mul = mul
exports.div = div

Here we have exported all 4 JavaScript functions separately. That’s means other Node JS project can reuse them directly.

在这里,我们分别导出了所有4个JavaScript函数。 这意味着其他Node JS项目可以直接重用它们。

It is a simple Arithmetic application. However if we take some realtime Node JS Applications, we can observe plenty of JavaScript files and each file may contain plenty of functions. In this scenario, it is very tedious process to export each and every function separately.

这是一个简单的算术应用程序。 但是,如果我们使用一些实时Node JS应用程序,则可以观察到很多JavaScript文件,并且每个文件可能包含很多功能。 在这种情况下,分别导出每个功能都是非常繁琐的过程。

To resolve this issue, we need to use Exporting a Node JS module technique as described below.

要解决此问题,我们需要使用如下所述的导出Node JS模块技术。

How to export a Node JS Module:

如何导出Node JS模块:

We use Node JS same “exports” object even to export a Node JS Module so that other Modules can reuse it. In realtime, it is recommended.

我们甚至使用Node JS相同的“导出”对象来导出Node JS模块,以便其他模块可以重用它。 建议您实时进行。

Syntax:

句法:

Example:

例:

Let us assume that we have created a simple Node JS Project with one JavaScript file: arthmetic.js file with the following content.

让我们假设我们已经创建了一个带有一个JavaScript文件的简单Node JS项目:arthmetic.js文件,其内容如下。

exports.arthmetic = {

var PI = 3.1416;
function add(a,b){
return a + b;
}

function sub(a,b){
return a - b;
}

function mul(a,b){
return a * b;
}

function div(a,b){
return a / b;
}
}

Here we have exported all 4 JavaScript functions and PI variable with just one exports statement. That’s means other Node JS project can reuse all functions and PI very easily.

在这里,我们仅用一个export语句导出了所有4个JavaScript函数和PI变量。 这意味着其他Node JS项目可以非常轻松地重用所有功能和PI。

导入Node JS模块 (Import a Node JS Module)

If we observe a Node JS Application or Module, it may dependent on other existing Node JS modules or our own Modules.

如果我们观察到Node JS应用程序或模块,则它可能依赖于其他现有的Node JS模块或我们自己的模块。

The major advantage of Modularity is reusability. We don’t need to redevelop the same existing functionality. We can import a Module and reuse that module functionality very easily.

模块化的主要优点是可重用性。 我们不需要重新开发相同的现有功能。 我们可以导入一个模块,并非常容易地重用该模块的功能。

How to import a Node JS Module:

如何导入Node JS模块:

We use same technique to import our modules or existing Node JS Modules.

我们使用相同的技术来导入我们的模块或现有的Node JS模块。

Node JS Platform has provided a function call “require()” to import one module into another.

Node JS平台提供了一个函数调用“ require()”,以将一个模块导入另一个模块。

Syntax:

句法:

Here module-name is our required Node JS module name.

这里module-name是我们必需的Node JS模块名称。

some-name is reference name to that module.

some-name是该模块的引用名称。

This require() call import the specified module and cached into the application so that we don’t need to import it again and again.

此require()调用导入指定的模块并缓存到应用程序中,因此我们不需要一次又一次地导入它。

Example:

例:

  • To import our own Node JS module
    var arthmetic = require("arthmetic");

    导入我们自己的Node JS模块
  • To import existing Node JS Module

    Import Node JS “express” module;

    var arthmetic = require("express");

    Import Node JS “mongoose” module;

    导入现有的Node JS模块

    导入Node JS“ express”模块;

    var arthmetic = require("express");

    导入Node JS“猫鼬”模块;

This require() call is similar to “import” statement in Java. We use import statement to import a package, class, interface etc into another class or interface.

此require()调用类似于Java中的“ import”语句。 我们使用import语句将包,类,接口等导入另一个类或接口。

Now we got some knowledge about how to export and import a Node JS module. We will use this knowledge to create our own Node JS Modules in next post.

现在我们了解了有关如何导出和导入Node JS模块的知识。 在下一篇文章中,我们将使用这些知识来创建自己的Node JS模块。

翻译自: https://www.journaldev.com/7599/nodejs-export-and-import-modules

nodejs导出导入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值