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 this module; packages It is 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 the package.json file. A package.json file that conforms to the CommonJS specification generally contains the following fields:
name: package name. Package names are unique and can only contain lowercase letters, numbers, and underscores. version: package version number. description: Package description. keywords: keyword array, used for search. homepage: project homepage. bugs: The address to submit bugs. license: license. maintainers: array of maintainers. contributors: array of contributors. repositories: array of project warehouse hosting addresses. dependencies: package dependencies.The package.json file can be edited manually or generated through the npm init command. 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.
Install the package through the command npm install xxx. for example:
Installation package: npm install express Update package: npm update express Delete package: npm uninstall expressFind the package in the npm community and 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 with JS in Node.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, etc.
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.
To simplify your operations, Node.js provides a special variable: exports equals module.exports. These two lines of code have the same effect:
module.exports.foo = foo;exports.foo = foo;Explain the 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. You can use module.exports and exports to export multiple members. You can only use module.exports to export a single member.
Recommended learning: "nodejs video tutorial"
The above is the detailed content of what nodejs modules and packages do. For more information, please pay attention to other related articles on this site!