Parse Node. js for module and package based code deployment

  • 2020-12-21 17:57:58
  • OfStack

Module path resolution rules

An experienced C programmer starts with an make file when writing a new program. Similarly, to get off to a good start with NodeJS, you need to have the directory structure and deployment of your code in place, just like you need scaffolding to build a house. This chapter will introduce the various knowledge related to it.

Module path resolution rules
We already know that the require function supports absolute paths beginning with a slash (/) or a disk character (C :), as well as relative paths beginning with./. However, these two paths create a strong coupling relationship between modules. Once the location of a module file needs to be changed, the code of other modules using the module also needs to be adjusted accordingly and become the whole body of pull 1. Thus, the require function supports a third form of path, written like foo/bar, and parses the path, in turn, according to the following rules, until the module location is found.

Built-in module

If the name of the NodeJS built-in module is passed to the require function, no path resolution is done and the exported object of the internal module is returned, such as require('fs').

node_modules directory

NodeJS defines a special node_modules directory for storing modules. Such as absolute path is a module/home user/hello js, using require in the module (' foo/bar) way to load module, the NodeJS, in turn, try to use the following path.


 /home/user/node_modules/foo/bar
 /home/node_modules/foo/bar
 /node_modules/foo/bar

NODE_PATH environment variable

Like the PATH environment variable, NodeJS allows additional module search paths to be specified via the NODE_PATH environment variable. The NODE_PATH environment variable contains 1 to multiple directory paths, separated by: under Linux and used under Windows; Space. For example, the following NODE_PATH environment variable is defined:

NODE_PATH=/home/user/lib:/home/lib
When loading a module using require('foo/bar'), NodeJS tries the following path in turn.


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

package

We already know that the basic unit of an JS module is a single JS file, but more complex modules tend to consist of multiple submodules. For ease of management and use, we can call a large module consisting of several sub-modules packages, and put all the sub-modules in the same directory.

In all the submodules that make up a package, there needs to be an entry module, and the export object of the entry module is treated as the export object of the package. For example, have the following directory structure.


- /home/user/lib/
  - cat/
    head.js
    body.js
    main.js

The cat directory defines a package that contains three submodules. As an entry module, ES77en. js has the following contents:


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 package's entry module. Then the above example, use the require ('/home user/lib cat/main ') to achieve a goal, but look in a entry module name appeared in the path is not a good idea. So we need to do a little extra work to make the package work more like a single module.

index.js
When the module file name is ES92en. js, the module can be loaded using the module directory path instead of the module file path, so the following two statements are equivalent to the above example.


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

Once this is done, you simply pass the package directory path to the require function, which feels more holistic as the entire directory is being used as a single module.

package.json
If you want to customize the filename and location of the entry module, you need to include an ES102en. json file in the package directory and specify the path to the entry module. 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

package. json is as follows.


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

So 1, that is you can use the same require ('/home user/lib/cat) load module. NodeJS finds the location of the entry module according to ES119en.json in the package directory.


Related articles: