node.js 模块
The author selected the Open Internet/Free Speech Fund to receive a donation as part of the Write for DOnations program.
作者选择了“ 开放互联网/言论自由基金会”作为“ Write for DOnations”计划的一部分来接受捐赠。
介绍 (Introduction)
In Node.js, a module is a collection of JavaScript functions and objects that can be used by external applications. Describing a piece of code as a module refers less to what the code is and more to what it does—any Node.js file or collection of files can be considered a module if its functions and data are made usable to external programs.
在Node.js中, 模块是可由外部应用程序使用JavaScript函数和对象的集合。 将一段代码描述为模块意味着更少的是什么代码,而更多的是它的功能-如果任何Node.js文件或文件集合可以使其功能和数据可用于外部程序,则可以将其视为模块。
Because modules provide units of functionality that can be reused in many larger programs, they enable you to create loosely coupled applications that scale with complexity, and open the door for you to share your code with other developers. Being able to write modules that export useful functions and data will allow you to contribute to the wider Node.js community—in fact, all packages that you use on npm were bundled and shared as modules. This makes creating modules an essential skill for a Node.js developer.
因为模块提供了可以在许多大型程序中重用的功能单元,所以它们使您能够创建松散耦合的应用程序,这些应用程序可以随着复杂性而扩展,并为您与其他开发人员共享代码打开了大门。 能够编写导出有用功能和数据的模块将使您能够为更广泛的Node.js社区做出贡献-实际上,您在npm上使用的所有软件包都捆绑并共享为模块。 这使得创建模块成为Node.js开发人员的一项基本技能。
In this tutorial, you will create a Node.js module that suggests what color web developers should use in their designs. You will develop the module by storing the colors as an array, and providing a function to retrieve one randomly. Afterwards, you will run through various ways of importing a module into a Node.js application.
在本教程中,您将创建一个Node.js模块,该模块建议Web开发人员在设计中应使用什么颜色。 您将通过将颜色存储为数组并提供随机检索颜色的功能来开发模块。 之后,您将通过各种方式将模块导入Node.js应用程序。
先决条件 (Prerequisites)
You will need Node.js and npm installed on your development environment. This tutorial uses version 10.17.0. To install this on macOS or Ubuntu 18.04, follow the steps in How To Install Node.js and Create a Local Development Environment on macOS or the Installing Using a PPA section of How To Install Node.js on Ubuntu 18.04. By having Node.js installed you will also have npm installed; this tutorial uses version 6.11.3.
您将需要在开发环境中安装Node.js和npm。 本教程使用版本10.17.0。 要将其安装在macOS或Ubuntu 18.04上,请遵循如何在macOS上安装Node.js并创建本地开发环境中的步骤,或如何在Ubuntu 18.04上安装Node.js的 使用PPA安装部分中的步骤 。 通过安装Node.js,您还将安装npm。 本教程使用版本6.11.3。
You should also be familiar with the
package.json
file, and experience with npm commands would be useful as well. To gain this experience, follow How To Use Node.js Modules with npm and package.json, particularly the Step 1 — Creating apackage.json
File.您还应该熟悉
package.json
文件,使用npm命令的经验也将很有用。 要获得这种经验,请遵循如何将Node.js模块与npm和package.json一起使用 ,尤其是步骤1 —创建package.json
文件 。It will also help to be comfortable with the Node.js REPL (Read-Evaluate-Print-Loop). You will use this to test your module. If you need more information on this, read our guide on How To Use the Node.js REPL.
熟悉Node.js REPL(读取-评估-打印循环)也将有所帮助。 您将使用它来测试您的模块。 如果您需要更多有关此方面的信息,请阅读有关如何使用Node.js REPL的指南 。
第1步-创建模块 (Step 1 — Creating a Module)
This step will guide you through creating your first Node.js module. Your module will contain a collection of colors in an array and provide a function to get one at random. You will use the Node.js built-in exports
property to make the function and array available to external programs.
此步骤将指导您创建第一个Node.js模块。 您的模块将在数组中包含一组颜色,并提供一种随机获取颜色的功能。 您将使用Node.js内置的exports
属性使函数和数组可用于外部程序。
First, you’ll begin by deciding what data about colors you will store in your module. Every color will be an object that contains a name
property that humans can easily identify, and a code
property that is a string containing an HTML color code. HTML color codes are six-digit hexadecimal numbers that allow you to change the color of elements on a web page. You can learn more about HTML color codes by reading this HTML Color Codes and Names article.
首先,首先要确定要存储在模块中的有关颜色的数据。 每种颜色都是一个对象,其中包含人类可以轻松识别的name
属性,以及一个code
属性,该属性是包含HTML颜色代码的字符串。 HTML颜色代码是六位十六进制数字,使您可以更改网页上元素的颜色。 您可以阅读此HTML颜色代码和名称文章,以了解有关HTML颜色代码的更多信息。
You will then decide what colors you want to support in your module. Your module will contain an array called allColors
that will contain six colors. Your module will also include a function called getRandomColor()
that will randomly select a color from your array and return it.
然后,您将决定要在模块中支持的颜色。 您的模块将包含一个名为allColors
的数组,该数组将包含六种颜色。 您的模块还将包含一个名为getRandomColor()
的函数,该函数将从数组中随机选择一种颜色并将其返回。
In your terminal, make a new folder called colors
and move into it:
在您的终端中,新建一个名为colors
文件夹并移至其中:
- mkdir colors mkdir颜色
- cd colors cd颜色
Initialize npm so other programs can import this module later in the tutorial:
初始化npm,以便其他程序可以在本教程的后面部分中导入此模块:
- npm init -y npm初始化-y