Home  >  Article  >  Web Front-end  >  What do nodejs modules and packages do?

What do nodejs modules and packages do?

WBOY
WBOYOriginal
2022-06-29 10:36:251516browse

In nodejs, a module is a js file used to use some specified functions. By dividing all functions into modules, the scope of global variables and functions defined in each module is also limited to Within this module; packages are used to manage modules and their dependencies, and can encapsulate multiple modules.

What do nodejs modules and packages do?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

What are nodejs modules and packages for?

Distinguish between packages and modules: Proper use of packages and modules will make your program less redundant, highly readable, and fully functional.

1. Packages in NodeJS

Packages are used to manage multiple modules and their dependencies. Multiple modules can be encapsulated. The root directory of the package must contain package. .json file.
A package.json file that complies with the CommonJS specification generally contains the following fields:

name:包名。包名是唯一的,只能包含小写字母、数字和下划线。
version:包版本号。
description:包说明。
keywords:关键字数组,用于搜索。
homepage:项目主页。
bugs:提交 bug 的地址。
license:许可证。
maintainers:维护者数组。
contributors:贡献者数组。
repositories:项目仓库托管地址数组。
dependencies:包依赖。

1.1 Generate a package.json file

The package.json file can be edited manually by yourself, or through npm init command to generate.
Enter the npm init command in the terminal to generate a package containing a package.json file. Directly enter npm init --yes to skip answering the question and directly generate the package.json file with default values.

1.2 Package operation

Use the command npm install xxx to install the package. For example:

  • Installation package:
npm install express
  • Update package:
npm update express
  • Delete package:
npm uninstall express

Find the package in the npm community, and then install it through the command npm install module name. The name of each module is globally unique.

2. Module in NodeJS

2.1 What is a module

We need to compare JS in the browser and Node.js JS:

  • In JavaScript, we usually divide the JavaScript code into several js files, and then merge and run these js files in the browser.
  • In Node.js, all functions are divided into modules. Each module is a js file, and the scope of global variables and functions defined in each module is also limited to this module. Only the exports object can be passed to external use.

Node.js officially provides many modules, each of which implements a function, such as the module fs for operating files and file systems, the module http for building http services, and the module path for processing file paths. wait.

2.2 Creation of module

We create a module and export it using module.exports.

myModule.js file

function foo() {
    console.log("hello syl");}module.exports.foo = foo;

index.js file

var hello = require("./myModule.js");hello.foo();

What do nodejs modules and packages do?

Note : The core module is defined in the lib/ directory of the Node.js source code. require() will always load core modules first.
For example: require('http') always returns the built-in HTTP module, even if there is a file with the same name.

2.3 Distinguish between module.exports and exports

Node.js provides a variable to simplify your operation: exports is equal to module.exports.
The effect of these two lines of code is the same:

module.exports.foo = foo;exports.foo = foo;

Explanation case:

// module.jsconsole.log('module', module)console.log('module.exports', module.exports)console.log('exports', exports)console.log(module.exports === exports);console.log("=========================================");exports = {
    a: 3,
  };console.log(exports);console.log(module.exports);console.log(exports === module.exports);

What do nodejs modules and packages do?

require() gets the value exported by module.exports , To export multiple members you can use module.exports and exports, to export a single member you can only use module.exports.

Recommended learning: "nodejs video tutorial"

The above is the detailed content of What do nodejs modules and packages do?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is nodejs multi-threaded?Next article:Is nodejs multi-threaded?