The implementation of node. JS's module and package mechanism is referenced by the CommonJS standard , but not fully followed. However, the difference between the two is not big, generally you do not have to worry about, only when you try to create a module or package that supports other platforms other than node. js, you need to study carefully. Usually, there is no direct conflict between the two places.
Modules are a basic part of the node. JS application, and the files and modules are one by one corresponding. In other words , a node. js file is a module that may be JavaScript code, JSON, or compiled C/s extensions . In the example in the previous section, we used the var http = require (' http '), where HTTP is a core module of node. JS, which is internally implemented in C + + and is encapsulated externally with JavaScript . We get the module through the Require function before we can use the object. The Require function obtains the module before it can use the object.
Create a Package
A package is a deeper abstraction on a module, and node. JS's package is similar to a C/s library or Java/.net
Class Library. It encapsulates a separate feature for publishing, updating, dependency management, and versioning. node. js Root
According to the CommonJS specification, the package mechanism was implemented and NPM was developed to solve the package release and acquisition requirements.
The node. js package is a directory that contains a JSON-formatted package description file Package.json. Strict character
Packages with the CommonJS specification should have the following characteristics:
? 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;
? The document should be in the doc directory;
? The unit test should be in the test directory.
node. JS's requirements for packages are not so stringent as long as the top-level directory is Package.json and compliant with some specifications
Can. Of course, in order to improve compatibility, we recommend that you strictly abide by the CommonJS specification when making the package.
1. Module as a folder
The module corresponds to a file of one by one. A file can be either a JavaScript code or a binary code, or it can be a folder. The simplest package is a module that acts as a folder. Let's take a look at an example of creating a folder called Somepackage, where you create a index.js with the following:
Somepackage/index.js
Exports.hello = function () {
Console.log (' Hello. ');
};
The Getpackage.js is then established outside the Somepackage, as follows:
Getpackage.js
var somepackage = require ('./somepackage ');
Somepackage.hello ();
Running node Getpackage.js, the console will output the result Hello:
We use this method to encapsulate a folder as a module, the so-called package. Packages are usually sets of some modules
provides a higher level of abstraction on the basis of a module, which is equivalent to a library of functions that provide some fixed interfaces. 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 file called Package.json, with content such as
is shown below:
{
"Main": "./lib/interface.js"
}
Then rename the index.js to Interface.js and put it under the Lib subfolder. Call this again in the same way
And can still be used normally.
When node. JS calls a package , it first checks the main field of the Package.json file in the package as
The interface module of the package, if the Package.json or main field does not exist, will attempt to find Index.js or Index.node as
the interface for the package .
Package.json is a CommonJS-defined document describing the package, fully conforming to the Package.json text of the specification.
Should contain the following fields.
? Name: The names of the packages must be unique, consisting of lowercase English letters, numbers, and underscores, and cannot contain
Space.
? Description: A brief description of the package.
? Version: Versions string that conforms to the semantic version recognition ① specification.
? Keywords: a key word group, typically used for search.
? Maintainers: Maintainer array, each element containing name, email (optional), web (optional)
Field.
? Contributors: The contributor array, in the same format as maintainers. The author of the package should be a contributor
The first element of the array.
? 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 (name of the license) and the URL (linked to
The address of the license text) field.
? Repositories: Warehouse managed address array, each element to contain type (warehouse type, such as Git),
The URL (the address of the warehouse) and the path (optional) field, relative to the warehouse.
? Dependencies: A dependency of a package, an associative array consisting of the package name and version number.
The following is an example of a package.json that fully conforms to the CommonJS specification:
{"name":"MyPackage","Description":"Sample Package for CommonJS. This package demonstrates the requiredElements of a CommonJS package.","version":"0.7.0","keywords": [" Package","Example"],"maintainers": [{"name":"Bill Smith","Email":"[email protected]",}],"Contributors": [{"name":"byvoid","Web":"http://www.byvoid.com/"}],"Bugs": {"Mail":"[email protected]","Web":"http://www.example.com/bugs"},"licenses": [{"type":"GPLv2","URL":"http://www.example.org/licenses/gpl.html"}],"repositories": [{"type":"git","URL":"Http://github.com/BYVoid/mypackage.git"}],"Dependencies": {"WebKit":"1.2","SSL": {"GnuTLS": ["1.0","2.0"],"OpenSSL":"0.9.8"}}}
node. JS modules and Packages