Write node foundation again (read node.js for a dozen C + + extensions)


Role of NPM: node JS program depends on the release, management and installation of packages.

Commonjs specification


Require is a function, the parameter is the module identifier, and the return value is the content exposed by the referenced module to external users.

To put it bluntly:

A module definition file, module1 js

    f: function(){

A reference module module1 JS file

let m1 = require('module1.js');
// {a: 1, f: ƒ}

Context of the module

In each module, there are some things as follows:

  1. Require function
  2. Exports object, and the things mounted on this object will be exposed to the outside.
  3. Module object, which contains some information of the module itself, such as ID

! [module object instance] ()

Note: the exports object actually points to module The reference of exports. If you replace it at willmodule.exportsThe actual references of the two will be different. If you don’t understand this problem, you may encounter some unpredictable problems in the use of century city.

Module identification

Module identification is a string, which should be named in the form of a small hump in theory, or with “.”, “..” And the path starting with “/”.
If the module ID is a file path, it is theoretically unnecessary to add the suffix “. JS” after the file name.

Unspecified contract

In accordance with the commonjs specification, there are some mechanisms that can be implemented at will. The reason for this is that in fact, these things are also necessary to build a complete module, but commonjs does not strictly define these things. In theory, as long as they can achieve the set goals.

  1. Module storage scheme, but the module content can be stored in database, file system, factory function and even a link library;
  2. The module loader can support the path environment variable for loading, but it can also not support addressing and is not subject to mandatory restrictions

Node. JS module

Node programs are usually assembled by modules. Possible forms of these modules: JS file, JSON file and binary file of C + + module.node

These modules are introduced through the require function.

Module lookup in node

The modules in node are divided into two categories: core module and file module.

The core module exists in the node source code and belongs to the node’s own module. Mainly some basic modules, such as:fs, httpWait. Their search is very simple. If the module identifier in require matches the name of these core modules, the content exported by these core modules will be returned directly.

File module is a module that needs to be obtained by searching for files. These modules are programs written by third-party developers and do not belong to node itself. They are the files you introduced when writing a node program (a program running based on node.js, not node.js self).

The file module can be divided intoThird party moduleandProject module

If the incoming require function is a file path, it can generally be regarded as a project module. For project modules, if specific files are specified in the path, they will be imported directly. If the path is a directory, first find out if it existspackage.jsonFile, if present and in package JSONmainProperty, the entry file of the module is searched according to the value of the main property. If not, the entry files under the directory are searched in turnindex.js, index.jsonas well asindex.node

If the value passed into the require function is a non path value, it can be regarded as a third-party module. Node also has a set of rules for finding third-party modules.

  1. Node of the current file (the file where the require function is located)_ Modules directory;
  2. Node under the parent directory of the current file_ Under modules, if not, continue to the parent directory to the node under the root directory_ modules;

After finding the corresponding module, it is actually equivalent to obtaining the file path of the module, and then it will find the entry file of the module (the search rules are the same as the project module) and obtain the contents of the module export.

Module cache

The node will cache the required module, that is, if a module has been loaded, when other modules reference the module, the node will directly read the contents in the cache and return.

Packet mechanism of node

Package description file package json

The main content has been written before.

Directory package structure

  • package. JSON is in the root directory
  • The binary files are placed in the bin directory
  • The javascript source code is placed in the worship directory
  • The document is placed in the doc directory
  • The unit test files are placed in the test directory

Node. JS / package under NPM

NPM(Node.js Package Manager)。

There is a package information description file in NPM – package json。 This file will record other dependencies of the package. And its third-party modules will be placed on node_ Modules directory, which leads some developers to submit this directory to the GIT repository as part of the project, but we don’t advocate doing so. Because node_ The files provided by modules are required by the project runtime, and usually, it has more contents. We can according to the package The description of the JSON file to install these dependencies before running the project (usually through the NPM commandnpm install)。

If you change the source code of the third-party module, it is recommended to hack at the time of project initialization or other appropriate time.

Package under NPM JSON file is implemented based on the package description file defined by commonjs. But it has some differences. We need to be clear about this.


In NPM, NPM2 is different from npm3. The dependent installation path of NPM2 is nested and suitable for node JS for tool and back-end development. In NPM2, if different packages use third-party modules with the same name but different versions, the two versions of this module are placed on the node of their respective references_ The modules directory does not interfere with each other.

    ---- bar
        ---- node_modules
            ---- [email protected]
    ---- baz 
        ---- node_modules
            ---- [email protected]

In npm3, the dependent installation path becomes flat. All third-party dependent packages are placed in the node under the root directory_ Modules. This method is suitable for front-end projects: it needs to be optimized to account for a small amount of code space and packaging analysis. At the same time, it also solves the problem that the file path in windows is not allowed to be too long.

Nesting occurs only when there is a conflict.

Npm3’s idea: flatten your dependence as much as possible.


Cnpm is the node of Alibaba group JS team developed a set of faster NPM tools. It is faster than NPM, not only by using domestic images, but also by caching some packages to node during installation_ modules/. Under the npminstall directory, connect the dependent directory to the corresponding path in the form of symbol link. In other words, in the actual installation process, the package of the same version has only one entity, similar to npm3.

Chrome V8

Node. JS uses chrome V8 as the interpreter of JavaScript.

V8 is an efficient JavaScript engine, which is different from other JS engines:

  1. JIT compilation
  2. garbage collection
  3. Introverted cache
  4. Hidden class

JIT compilation

JIT compilation, just in time compilation, instant compilation. The compiled output is machine language, not bytecode.

garbage collection

V8’s garbage collector draws lessons from the precise garbage collection management of Java VM, and the efficiency of its garbage collection mechanism is quite high.

Inline cache

If A.B is accessed, the hash table will not be addressed again when A.B is accessed again, because V8 has cached the offset of this attribute and does not need to calculate the addressed offset again.

Hidden class

Todo: the specific mechanism of the hidden class needs to be further queried.

The more efficient mechanism of V8 engine is mainlyinline caching andHidden class

The V8 engine follows the ECMA script standard. Node. JS will upgrade its own code with the update of V8.


Libuv is a cross platform class library that focuses on asynchronous I / O. It was originally written for node by Ryan Dahl, the founder of node. As long as it is developed for node, it can also support other projects.

An important concept in libuv isevent loop

Some features of libuv

  • An all-round event loop based on epoll / kqueue / IOCP / event ports;
  • Asynchronous TCP and UDP sockets
  • Asynchronous DNS resolution
  • Asynchronous file, file system operation
  • File system events
  • TTY controlled by ANSI escape code
  • Socket sharing IPC using UNIX domain sockets or named pipes
  • Subprocess
  • Thread pool
  • Signal processing
  • High precision clock
  • Thread and synchronization element

PS: different platforms have different asynchronous mechanisms (such as epoll, IOCP, etc.), based on which libuv implements cross platform event loops.

In addition, libuv interface is concise, flexible and easy to use. The event loop in node directly uses the event loop of libuv.

Other dependencies in node


An HTTP message parser implemented in C can parse the request data and return data of HTTP protocol.


Secure socket layer protocol library.
SSL (secure socket layer) can provide secret transmission on the network. Used to encrypt the HTTP protocol.


Zlib is a library that provides data compression.


God’s editor and God of Editors

  • VIM/NeoVIM
  • Emacs
  • Sublime Text
  • Visual Studio Code
  • Atom
  • Visual Studio


Node gyp is node JS C + + extension construction tool. Work based on gyp (general your projects). Gyp is a set of construction tools of Google.

Node gyp needs some dependencies to work.

On UNIX series platforms:

  • python
  • make
  • C + + compiler Toolkit (GCC)

Under MacOS:

  • xcode

In Windows

  • Visual Studio

Or use Visual C + + build tools (relatively lightweight).