node. JS: Package

Source: Internet
Author: User

Summary : This blog mainly describes the packages in node. js

A package is a deeper abstraction on the basis of a module, and node. JS's package is similar to the library function or java/. NET's class library. It encapsulates a separate feature for publishing, updating, dependency management, and versioning. The node. js package is a directory that contains a JSON-formatted package description file Package.json. Packages that strictly conform to 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.

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 the module that acts as a folder. Let's take a look at an example: Create a folder called Somepackage, where the index.js is created, with the following:

function () {    console.log (' Hello ');};

The Getpackage.js is then established outside the Somepackage, as follows:

var somepackage = require ('./somepackage '); Somepackage.hello ();

Run the Getpackage.js console to output the result: Hello.

We use this method to encapsulate a folder as a module, the so-called package. By customizing the Package.json, we can create more complex, more complete, more compliant packages:

In the previous example, under the Somepackage folder, we created a file called Package.json, which reads as follows:

{    "main": "./lib/interface.js"}

Then rename the index.js to Interface.js and put it under the Lib subfolder. Call the package again in the same way, and it will still work.

When node. JS calls a package, it first checks the main field of the Package.json file in the package as an excuse module for the package, and if the Package.json or main field does not exist, it tries to find Index.js or Index.node as the interface for the package.

A fully compliant Package.json file should have the following fields:

Name: The names of the packages must be unique, consisting of lowercase English letters, numbers, and underscores, and cannot contain spaces.

Description: A brief description of the package.

Version: A string of versions 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 the first element of the contributor array.

Bugs: The address where the bug was submitted can be a URL or an email address.

Licenses: An array of licenses, each element to contain the type (license name) and URL (the address that is linked to the license text) field.

Repositories: An array of warehouse-managed addresses, each of which contains the type (types of warehouses, such as git), the URL (the address of the warehouse), and the path (optional) field as opposed 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 required elements of a CommonJS.",    "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: Package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.