Concept: Modules (module) and packages are the most important pillars of node. js. It is not possible to develop a program of a certain size using only one file, usually by splitting, assembling, and then combining the various functions. The module was formally created to achieve this, and in browser JavaScript, the split and composition of the script module is usually implemented using the HTML script tag, and node. JS provides the Require function to invoke the other modules, andmodules are file-based, the mechanism is very simple, the difference between modules and packages is transparent, so it is often not differentiated. Module 1. What is a module The modules and files are one by one corresponding. A node. js file is a module that may be JavaScript code, JSON, or compiled C/s extensions. var http=require (' http '), where HTTP is a core module of node. js, gets the module through the Require function and then uses the object in it 2. Creating and Loading modules (1) Creating Modules node. JS provides exports and require two objects, where exports is the interface that the module exposes, require is used to obtain an interface for a module from the outside, that is, to get the exports object of the module through two JS files at the same level: Module.js and Getmodule.js implementations:
Operation Result:
(2) A single load the above example is a bit like creating an object, but actually it is fundamentally different from the object becauserequire does not load modules repeatedly, that is, no matter how many times require is called, the module gets the same change ⑴ getmodule.js to getmodule2.js: run result: &nb sp; (3) Cover exports sometimes we just want to encapsulate an object in a module, such as Define Module:singleobejct.js Introducing modules using:getsingleobject.js A. Tedious:exports.hello=hello; Introduction: Require ("./singleobject") .hello; b. Easy: module.exports= Hello
Operation Result:
Exports itself is just an ordinary empty object, {}, which is specifically used to declare interfaces II, create packages 1. Package Concept The package is a deeper abstraction on the module, and node. JS's package is similar to a C/s library or a Java class library, which speaks of a separate function encapsulated for publishing, updating , management-dependent versioning. NPM has been developed to address the release and acquisition requirements of packages. 2. How to create a package            COMMONJS specification package should have a feature: package.json must be in the top-level directory of the package binary files should be in the bin directory The javascript code should be in the Lib directory documentation should be in the doc directory unit tests should be in the test directory node.js The package requirements are not so strict, as long as the top-level directory has Package.json, and conforms to the basic specifications can (1) Modules as folders Somepackage folder 's simplest package is a module that acts as a folder, Create a folder Somepackage, There is a index.js inside, which provides a way to SayHello () var Somepackage=require ('./somepackage '); somepackage.sayhello (); We use this method to encapsulate a folder into a module, the so-called package. A package is usually a collection of modules that provide a higher level of abstraction on the basis of a module, equivalent to a library of functions that provide fixed interfaces, and by customizing Package.json we can create more complex, more sophisticated, more compliant packages for publishing.
(2) package.json in the previous example, under the Somepackage folder, we created a Package.json file, content { ' main ': './lib/index.js ' } When node. JS calls a package, it checks the main field of the Package.json file in the package as an interface module for the package, and if the Package.json or main field does not exist, it will look for index.js or index. Code as the interface for the package Package.json:
Note the path and file format package.js:
The canonical properties of the package.json: name: The name of the package must be unique description: Brief description of the package version: Version strings that conform to the semantic version recognition specification keywords: keyword data, typically used for search maintainers: Maintainer array, each element containing name, Email, web optional field    &Nbsp; contributors: The contributor array, in the same format as maintainers. The sitting of the package should be the first element of the contributor's data bugs: The address where the bug was submitted can be a URL or an email address licenses: The license array, each element to contain the type and URL fields repositories: Warehouse managed Address Array, Each element must contain type, URL, and path fields dependencies: A dependency of a package, an associative array consisting of the package name and version number. ⑶ new Getpackage.js, running result:
"section fourth node. js modules and Packages"