Node. js module, package management and development

Source: Internet
Author: User

When it comes to components, JavaScript does not have this feature. Many functions of Javascript in the past were implemented by different vendors. Components have the following features:

. JavaScript does not have a module system. There is no native support for confined scope or dependency management.

. JavaScript does not have a standard library. There are no file system APIs in the core library, but these will be available after html5 :).

. JavaScript does not have a standard API.

. JavaScript does not have a package management system.

With the advent of commonjs (http://www.commonjs.org) specifications, the goal is to build an ecosystem of Javascript in terms of web servers, desktops, command line tools, and browsers.

Commonjs has developed some specifications to solve these problems, and node. JS is an implementation of these specifications. Node. js implements the require method as a method for introducing modules. Meanwhile, NPM implements dependency management, automatic module installation, and other functions based on the package specification defined by commonjs. Here we will go into node. js's require mechanism and NPM's package-based application.

I. node. js module package management:

1. Write and load modules

Writing and installing a JS module in node. JS is very easy.

First write a test. js file with the following content:

Exports. myfunc = function (){
Console. Log ('hello, World ');

};

You can also write it as follows:

VaR myfunc = function (){

Console. Log ('hello, World ');

};

Exports. myfunc = myfunc;

Save this file and call it in your other JS Code. Here I write an app. js with the following content:

VaR test = require ('./test. js ');
Console. Log ('test my function: \ n' + test. myfunc ());

Run:

Node app. js

You will find that:

Test my function:

Hello World

The following background actions are performed when you use node app. JS:

First, node uses the module object to load your app. js.

Then, your JS content will be encapsulated and called. The original content in APP. JS is changed from:

(Function (exports, require, module, _ filename, _ dirname ){

VaR test = require ('./test. js ');
Console. Log ('test my function: \ n' + test. myfunc ());

})

Then execute. When you execute require ('./test. js'), search for test. js by path.

In fact, this require method actually calls the load method of the module object. After loading, compiling, and caching a module, the load method returns the module's exports object. This is why only the methods defined on the exports object in the test. js file can be called externally.

In fact, the require loading module has a complicated logic.

Here, we will give a brief introduction,

First case. When the parameter in require is a string without a path and a file suffix, for example, require ('httpd ');

The node. js search sequence is as follows:

A. load from the cache. B. Load. C from the native module. load from the file. Loading from a file is also in a very sequential order:

Node. js runs a JS program. For example, when node app. JS is used just now, a valid path is formed during module loading, which is a path set.

First, the current path, then the parent directory of the current path, and then the parent directory of the parent directory, only to the root directory.

The search module first looks for the node_modules sub-directory in the current path. If no package is found, the package in this directory is found. the directory specified by the main parameter in the JSON file is then the node_modules sub-directory of the parent directory. The root directory is always found and cannot be found. Find the directory set in node_path.

The second case: the require parameter is the module name in the relative or absolute path, such as require ('. /test. JS ') or require ('/home/joezhang/nodejs/test') does not need to be searched and loaded directly, because there is an absolute path :)

2. package installation:

The package in node. JS is the packaging module. In general, we can install some packages. Use NPM install for installation. NPM install package-name is installed in the node. js package resource library. You can use the following command to view the resource library Server:

$ NPM config get Registry

Of course, you can also change this library $ NPM config set registry "myregistry.xxx.com" as described in another blog

In fact, you can also download the package from the network, decompress the package, and install it locally.

Generally, the downloaded package is a directory, which determines that a package. JSON file exists. Run the following command to install the SDK:

NPM install package-path for example: NPM install '/home/joezhang/Program/mypkg'

The mypkg directory contains a package. JSON file conforming to the commonjs specification.

 

2. module development:

1. module category:

Native:

Non-native:. js. node. JSON.

2. there are two types of module development: one is mentioned. for JS module development, you can also directly load this JS file module to your project, or encapsulate it into a package format and install it to your node. JS is used in the global environment.

JS module development has been mentioned before. The focus here is on the development and use of third-party modules in C/C ++.

--- To be continued ----

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.