Home > Web Front-end > JS Tutorial > Analysis of Node.js module- and package-based code deployment method_node.js

Analysis of Node.js module- and package-based code deployment method_node.js

WBOY
Release: 2016-05-16 15:15:22
Original
1261 people have browsed it

Module path resolution rules

Experienced C programmers start with the make file when writing a new program. Similarly, before using NodeJS to write a program, in order to get a good start, you first need to prepare the directory structure and deployment method of the code, just like building a scaffolding when repairing a house. This chapter will introduce various related knowledge.

Module path resolution rules
We already know that the require function supports absolute paths starting with a slash (/) or a drive letter (C:), and also supports relative paths starting with ./. However, these two paths establish a strong coupling relationship between modules. Once the storage location of a module file needs to be changed, the code of other modules that use this module also needs to be adjusted accordingly, which becomes a problem. Therefore, the require function supports the third form of path, written similarly to foo/bar, and parses the path according to the following rules in turn until the module location is found.

Built-in modules

If the name passed to the require function is a NodeJS built-in module name, path analysis will not be performed and the exported object of the internal module will be returned directly, such as require('fs').

node_modules directory

NodeJS defines a special node_modules directory for storing modules. For example, the absolute path of a module is /home/user/hello.js. When the require('foo/bar') method is used to load the module in the module, NodeJS will try to use the following paths in sequence.

 /home/user/node_modules/foo/bar
 /home/node_modules/foo/bar
 /node_modules/foo/bar
Copy after login

NODE_PATH environment variable

Similar to the PATH environment variable, NodeJS allows specifying additional module search paths through the NODE_PATH environment variable. The NODE_PATH environment variable contains one or more directory paths. The paths are separated by : under Linux and ; under Windows. For example, the following NODE_PATH environment variable is defined:

NODE_PATH=/home/user/lib:/home/lib
When using require('foo/bar') to load a module, NodeJS tries the following paths in sequence.

 /home/user/lib/foo/bar
 /home/lib/foo/bar

Copy after login

Pack

We already know that the basic unit of a JS module is a single JS file, but more complex modules are often composed of multiple sub-modules. In order to facilitate management and use, we can call a large module composed of multiple sub-modules a package, and put all sub-modules in the same directory.

Among all the sub-modules that make up a package, there needs to be an entry module, and the export object of the entry module is used as the export object of the package. For example, there is the following directory structure.

- /home/user/lib/
  - cat/
    head.js
    body.js
    main.js
Copy after login

The cat directory defines a package, which contains 3 sub-modules. main.js serves as the entry module and its content is as follows:

var head = require('./head');
var body = require('./body');

exports.create = function (name) {
  return {
    name: name,
    head: head.create(),
    body: body.create()
  };
};

Copy after login

When using a package in other modules, you need to load the entry module of the package. Continuing with the above example, using require('/home/user/lib/cat/main') can achieve the goal, but it does not seem like a good idea if the entry module name appears in the path. So we need to do a little extra work to make the package behave more like a single module.

index.js
When the file name of the module is index.js, when loading the module, you can use the path of the directory where the module is located instead of the module file path. Therefore, following the above example, the following two statements are equivalent.

var cat = require('/home/user/lib/cat');
var cat = require('/home/user/lib/cat/index');
Copy after login

After processing in this way, you only need to pass the package directory path to the require function. It feels like the entire directory is used as a single module, giving a more holistic feel.

package.json
If you want to customize the file name and storage location of the entry module, you need to include a package.json file in the package directory and specify the path of the entry module in it. The cat module in the above example can be refactored as follows.

- /home/user/lib/
  - cat/
    + doc/
    - lib/
      head.js
      body.js
      main.js
    + tests/
    package.json
Copy after login

The content of package.json is as follows.

{
  "name": "cat",
  "main": "./lib/main.js"
}
Copy after login

In this way, you can also use require('/home/user/lib/cat') to load the module. NodeJS will find the location of the entry module based on package.json in the package directory.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template