Nodejs module and loading mechanism, mainly discuss the problem of unable to find module

Source: Internet
Author: User
Tags install node

  

  The main idea, loading the module is simply to find out where the module, as long as the location of the module and module loading logic so can not find the module problem is solved. This article only synthesizes oneself the knowledge point to carry on the summary, inevitably appears the understanding wrong place, please forgive me.

Module Classification of Nodejs

1, the native module: HTTP fs path, and so on, these modules are in the source package of the Lib directory, Nodejs after installation is not found these modules, As part of the Node.exe, require these modules will never be a problem, if the day of the problem, restart the computer directly or re-install node. What are the questions? You can view the features of these native modules by downloading the source code. Address:https://nodejs.org/download/

2, File module: Third-party modules, such as Express, the modules installed through NPM are third-party modules. These are the modules that are often not found.

3, File module: We write the module, the reason is independent because it is loaded and the other two kinds of modules are different.

How to define a module

Nodejs declares a module that has 2 practices

    Exports.module_name

    Module.exports

The difference between the two is also very simple, but to understand very laborious, the key point is to know that there is a module This global variable exists, print out and make a few attempts, it is fully understood, here is a very wonderful The similarities and differences between the two articles: http://www.cnblogs.com/kongxianghai/p/3936197.html

Where is the third-party module installed (NPM)

Several commands

//View Set the global installation directory, the globally installed module installs the NPM install [-g] global installation under the Node_modules directory under the directory,  and the module will be installed in the global directory. Without-G is installed directly in the current directory, which is installed locally

The existence form of the module

1, the file contains, this is more intuitive, directly assigned to the file name (minus the. js suffix), you can get all the exported modules inside the file.

 2, the folder contains, the third-party module installed through NPM is this way, designated to the module in the folder, which is the module name, in Express as an example:

    

Load code:var ex = require (' Express ')

Loading mechanism:

First, search for the package.json file in the current directory, find the Mian property inside, and, if present, load the file specified by the property.

If there is no Package.json or there is no main field in the file, Nodejs will attempt to load the index.js

It doesn't exist. Then there's only one cannot find module.

where modules are searched (for third-party modules)

  1, step up search

Here is a global variable that contains all the information that the Nodejs process runs in, and prints it here process.mianmodule

    

Obviously, Nodejs will start from the current directory to search for node_modules, it is of course best, can not find that will continue to find Node_path this environment variable.

2. Environment variable Node_path

If there is a node_path in the environment variable and the value of the variable is a globally installed directory, then there will be no error finding the module.

There are no problems with the node_path of the IDE, such as Eclipse, sublime, or, if you encounter them, configure them individually. Of course, the plugin must be installed first.

    Eclipse

Right-click Run as--run configurations ... Find the following interface

    

    Sublime

Preferences, Package Settings, Nodejs-Settings-default

    

Fill in the Node_path.

Require (' How to fill in here ')

1, relative path to specify the module, generally used to load their own modules.

Symbols that must be used: ./ represents the current directory, relative to the path is the current directory

.. / represents the previous level module, can be used indefinitely until you jump to the root directory

These two symbols must start with one, for example, to load another module in the current directory, assuming that the file name Hello.js is currently in the file is hi.js we need to write this in the Hi.js file:

    var hello = require ('./hello ')//variable name arbitrary, general usage file name

On this basis, and then add the name of the folder, we can load to our own write any module.

2, the absolute path specifies the module address, in addition to the native module, any file module can be loaded, unless the path error. For example, we can load the Express module like this, taking the path of my express module as an example.

  

In this case, you can also change the name of Express, for example, I changed the name of the Express folder to ex

  var ex = require (' F:\\nodejs\\node_modules\\ex ')

is also possible.

3, directly use require (' xxx ') then the loaded module is either a native module, or the module under a node_modules directory

    

    

Nodejs module and loading mechanism, mainly discuss the problem of unable to find module

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.