In the Node.js language, there is no essential difference between packages and modules. Packages are a deeper abstraction based on modules. Packages encapsulate an independent function and are used for publishing, updating, dependency management and Perform version control. Node.js implements the package mechanism according to the CommonJS specification, and npm was developed to solve the package publishing and acquisition requirements.
The package of Node.js is a directory that contains the package description file package.json in JSON format. The package of Node.js basically follows the CommonJS specification, so it has the following characteristics:
Package features defined by the CommonJS specification:
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) The doc directory stores documents;
5) The test directory stores unit tests.
Node.js modules and files have a one-to-one correspondence. Files can not only be JavaScript source files or binary files, but also directories. The simplest package is a directory module.
Node.js packages are usually a collection of modules, which provide a higher level of abstraction based on the modules, which is equivalent to a function library that provides some fixed interfaces.
By customizing package.json, we can create more complex, more complete, and more compliant packages for release.
When Node.js calls a package, it will first check the main field of the package.json file in the package and use it as the interface module of the package. If the main field of the package.json file does not exist, then Node.js will try Look for index.js or index.node as the interface of the package.
The package.json file is a file used by the CommonJS specification to describe a package. A package.json file that fully conforms to the specification should contain the following fields:
1) name: package name. The package name is unique and consists of lowercase letters, numbers and underscores, and cannot contain spaces.
2) description: package description. Give a brief description of the package.
3) version: version number. A version string that meets the "Semantic Version Identification" specification.
4) keywords: array of keywords, usually used for search.
5) maintainers: array of maintainers. Each element contains name, email (optional), web (optional) fields.
6) contributors: array of contributors. The format is the same as the maintainer array. The package author should be the first element of the contributors array.
7) Bugs: The address to submit bugs, which can be a website or email address.
8) licenses: license array. Each element should contain type (license name) and url (address link to the license text) fields.
9) repositories: array of warehouse hosting addresses. Each element must contain type (type of warehouse, such as Git), url (warehouse address) and path (path relative to the warehouse, optional) fields.
10) dependencies: package dependencies. Is an associative array consisting of package name and version number.
Note: The "Semantic Version Identification" specification is a set of version naming specifications proposed abroad. The original purpose was to solve various version number size comparison problems. It is currently adopted by many package management systems.
The following is a package.json example that fully complies with the CommonJS specification: