Home >Web Front-end >Front-end Q&A >What do nodejs modules and packages do?
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.
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
Distinguish between packages and modules: Proper use of packages and modules will make your program less redundant, highly readable, and fully functional.
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:包依赖。
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.
Use the command npm install xxx to install the package. For example:
npm install express
npm update express
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.
We need to compare JS in the browser and Node.js JS:
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.
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();
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.
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);
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!