This article mainly introduces the parsing of Node. js Code deployment method based on modules and packages. As a JavaScript interpreter serving the local operating system, Node adopts a concise deployment method based on the module and package concepts, for more information, see
Module path parsing rules
Experienced C programmers first write from the make file when writing a new program. Similarly, before using NodeJS to write a program, in order to have a good start, you must first prepare the directory structure and deployment method of the Code, just as building scaffolding before building a house. This chapter describes the related knowledge.
Module path parsing rules
As we know, the require function supports absolute paths starting with a slash (/) or a drive letter (C :), and relative paths starting with a slash. 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 using this module also needs to be adjusted, become a whole body. Therefore, the require function supports the third form of path, which is similar to foo/bar, and resolves the path according to the following rules until the module location is found.
Built-in modules
If the node. js built-in Module name is passed to the require function without path parsing, the export object of the internal module is directly returned, for example, 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 module is loaded using the require ('foo/bar') method, NodeJS tries to use the following path in sequence.
/home/user/node_modules/foo/bar /home/node_modules/foo/bar /node_modules/foo/bar
NODE_PATH environment variable
Similar to the PATH environment variable, NodeJS allows you to specify an additional module search PATH through the NODE_PATH environment variable. The NODE_PATH environment variable contains one or more directory paths separated by: in Linux and in Windows. For example, the following NODE_PATH environment variable is defined:
NODE_PATH =/home/user/lib:/home/lib
When the module is loaded using require ('foo/bar'), NodeJS tries the following paths in sequence.
/home/user/lib/foo/bar /home/lib/foo/bar
Package
We already know that the basic unit of the JS module is a single JS file, but complicated modules are usually composed of multiple submodules. To facilitate management and use, we can package a large module consisting of multiple sub-modules and put all sub-modules in the same directory.
In all sub-modules that constitute a package, an entry module is required. The export object of the entry module is used as the export object of the package. For example, the following directory structure is available.
- /home/user/lib/ - cat/ head.js body.js main.js
The cat directory defines a package, which contains three sub-modules. As the entry module, main. js has the following content:
var head = require('./head');var body = require('./body');exports.create = function (name) { return { name: name, head: head.create(), body: body.create() };};
When using packages in other modules, You need to load the entry module of the package. Next, in the above example, you can use require ('/home/user/lib/cat/main') to achieve the goal, but it does not seem a good idea to show the name of the entry module in the path. Therefore, we need to do some extra work to make the package more like a single module.
Index. js
When the module File Name Is index. js, you can use the path of the Module Directory to replace the module file path when loading the module. Therefore, in the preceding example, the following two statements are equivalent.
var cat = require('/home/user/lib/cat');var cat = require('/home/user/lib/cat/index');
After this process, you only need to pass the package directory path to the require function. It feels that the entire directory is used as a single module and has a more overall sense.
Package. json
To customize the file name and storage location of the entry module, you must include a package. json file in the package directory and specify the path of the entry module. In the preceding example, the cat module can be reconstructed as follows.
- /home/user/lib/ - cat/ + doc/ - lib/ head.js body.js main.js + tests/ package.json
The contents of package. json are as follows.
{ "name": "cat", "main": "./lib/main.js?1.1.9"}
In this way, you can use require ('/home/user/lib/cat') to load modules. NodeJS finds the position of the entry module based on package. json in the package directory.