Node. js Lesson 5 (modules and packages)

Source: Internet
Author: User

Zookeeper concept: modules and packages are the most important pillar of Node. js. Developing a program of a certain scale cannot only use one file. Generally, You need to split, package, and combine various functions. The module was born to implement this method. In the browser JavaScript, the splitting and combination of the script module is usually implemented using the HTML script tag, Node. js provides the require function to call other modules. The module is based on files and the mechanism is very simple. The difference between modules and packages is transparent, so it is often not distinguished. I. Module 1. What is a one-to-one correspondence between module modules and files. A Node. js file is a module, which may be JavaScript code, JSON, or compiled C/C ++ extensions. Var http = require ('HTTP '), where http is Node. A core module of js, obtain this module through the require function, and then use the object 2. create and load a module (1) create a module Node. js provides two objects: exports and require. exports are public interfaces of the module, and require is used to obtain an interface of a module from the outside, that is, obtain the exports object of the module (2) the example above is a bit similar to creating an object at a time, but it is essentially different from the object because require does not reload the module, that is, no matter how many requests are called, the obtained modules are the same (3) overwrite exports. Sometimes we want to encapsulate an object in a module, for example, define a module: singleobejct. use the js import module: getSingleObject. js tedious: exports. hello = hello; introduced: require (". /singleobject "). Hello; simple: module. exports = hello; exports itself is only a common empty object, that is, {}, which is specifically used to declare interface 2. Create Package 1. package concept package is a deeper abstraction on the basis of the module, Node. the js package is similar to the C/C ++ function library or java class library. It encapsulates an independent function for Version Control of release, update, and dependency management. Npm is developed to meet the packet release and acquisition requirements. 2. how to Create a package CommonJS standard package should have the following features: package. json must be in the package's top-level directory. The binary file 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 Node under the test directory. js has less stringent requirements on packages, as long as there is a package under the top-level directory. json, and comply with the basic specifications. (1) The simplest package of the somepackage folder, which is used as the folder module. Create a folder named somepackage with an index. js, which provides a method for sayHello () var somepackage = require ('. /somepackage '); somepackage. sayHello (); this method can be used to encapsulate a folder into a module, that is, a package. A package is usually a set of modules. It provides high-level abstraction based on the module, which is equivalent to providing some fixed interface function libraries by customizing the package. json. We can create more complex, more comprehensive, and more compliant packages for release. (2) package. in the preceding example of json, In the somepackage folder, we create a package. json file with {'main ':'. /lib/index. js'} Node. when js calls a package, it checks the package in the package. the main field of the json file, which is used as the interface module of the package. if the json or main field does not exist, the system will try to find the index. js or index. code is the package interface of the package. json standard attribute: name: package name, which must be unique description: Brief description of the package version: version string that complies with semantic version recognition specifications keywords: keyword data, it is usually used to search for maintainers: maintainer array. Each element must contain the name, email, and web optional field contributors: contributor array. The format is the same as that of maintainers. The package sat should be the first element of the contributor data bugs: the address for submitting bugs, which can be the url or email address licenses: License array. Each element must contain the type and url fields repositories: repository hosting address array. Each element must contain the type, url, and path field dependencies: package dependency. An associated array consists of the package name and version number.

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.