Parse the code deployment method of Node. js Based on modules and packages, and deploy node. js

Source: Internet
Author: User
Tags install node

Parse the code deployment method of Node. js Based on modules and packages, and deploy node. js

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"}

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.

Articles you may be interested in:
  • How to Use npm to publish the Node. JS package
  • When node. js uses npm to install the plug-in, it prompts "install Error: ENOENT". How can this problem be solved?
  • Nodejs npm package. json Chinese Document
  • Node. js installation tutorial and NPM Package Manager usage
  • My Node. js learning path (2) NPM module Management
  • Nodejs npm package management configuration method and Common commands
  • Nodejs npm install global installation and local Installation
  • Node. js generates the HttpStatusCode auxiliary class and publishes it to npm.
  • Use the Package Manager in linux to install node. js
  • Detailed description of the Node. js package project directory and the use of the NPM Package Manager

Related Article

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.