Node. js package details, Node. js package details
In Node. in js language, packages and modules are not essentially different. packages are further abstracted Based on modules. Packages encapsulate an independent function, it is used for release, update, dependency management, and version control. Node. js implements the package mechanism according to CommonJS specifications, and develops npm to meet the package release and acquisition requirements.
The Node. js package is a directory containing the package. JSON file in json format. The Node. js package follows the CommonJS specification, so it has the following features:
Package features defined by CommonJS specifications:
1) the top-level directory contains the package. json file;
2) The bin directory stores binary files;
3) The lib directory stores JavaScript files;
4) store documents in the doc directory;
5) store the unit test in the test directory.
Node. js modules correspond to files in a one-to-one manner. files can not only be JavaScript source code files or binary files, but also directories. The simplest package is a directory module.
The Node. js package is usually a collection of some modules. It provides high-level abstraction on the basis of the module, which is equivalent to providing some fixed interface function libraries.
By customizing package. json, we can create more complex, more comprehensive, and more compliant packages for release.
Node. when js calls a package, it first checks the package in the package. the main field of the json file, which is used as the interface module of the package. the main field of the json file does not exist, so Node. js will try to find the index. js or index. node is the package interface.
The package. json file is the file used to describe the package in CommonJS specifications. The fully compliant package. json file should contain the following fields:
1) name: package name. The package name is unique and consists of lower-case letters, numbers, and underscores. It cannot contain spaces.
2) description: Package description. Briefly describe the package.
3) version: version number. The version string that meets the semantic version recognition specification.
4) keywords: a keyword array, usually used for search.
5) maintainers: the maintainer array. Each element contains the name, email (optional), and web (optional) fields.
6) contributors: A contributor array. The format is the same as that of the maintainer array. The package author should be the first element of the contributor array.
7) bugs: the address for submitting a bug. It can be a website or email address.
8) licenses: License array. Each element must contain the type (license name) and url (url linked to the license text) fields.
9) repositories: array of repository hosting addresses. Each element must contain the type (repository type, such as Git), url (repository address), and path (optional) fields.
10) dependencies: package dependency. Is an associated array consisting of the package name and version number.
Note: The semantic version recognition specification is a set of version naming rules proposed by foreign countries. Its initial goal is to solve various version number comparison problems and is currently used by many package management systems.
The following is an example of package. json that fully complies with CommonJS specifications:
Copy codeThe Code is as follows:
{
"Name": "testpackage ",
"Description": "My package for CommonJS .",
"Version": "0.1.0 ",
"Keywords ":[
"Testpackage ",
"Liq"
],
"Maintainers ":[
{
"Name": "liq ",
"Email": "liq@hotmail.com ",
}
],
"Contributors ":[
{
"Name": "liq ",
"Web": "http://blog.csdn.net/chszs"
}
],
"Bugs ":{
"Mail": "liq@hotmail.com ",
"Web": "http://blog.csdn.net/chszs"
},
"Licenses ":[
{
"Type": "Apache License v2 ",
"Url": "http://www.apache.org/licenses/apache2.html"
}
],
"Repositories ":[
{
"Type": "git ",
"Url": "http://github.com/chszs/packagetest.git"
}
],
"Dependencies ":{
"Webkit": "1.2 ",
"Ssl ":{
"Gnutls": ["1.0", "2.0"],
"Openssl": "0.9.8"
}
}
}