About require
Whenever a node.js/io.js is used, this function must be used to require
refer to other modules (written by themselves or others). But a lot of people (including myself) did not give a serious look at node about require ( modules This part of the document).
Here's a look at some of the things you might have known:
File loading
When you use require to load a file in node, you try to add .js
,. node
, and three suffix names in the case where the reference file name does not exist .json
. Note that this refers to the case where the reference file name does not exist.
For example, when you use the Code require(‘./hello‘)
to refer to this file in the current directory hello
, it will default to find the name of hello
the file exists, if there is a file (that is, without any suffix), then it will directly read the contents of the file, instead of trying to add a suffix. It is also important to note that it is here to interpret this suffix as a JS file, regardless of what code is in it.
For path problems when referencing files, resolve by .
and /
by relative path or absolute path.
From
Node_modulesLoad
If used require
without any relative or absolute path, node tries to node_modules
find the required module from the. Node Lookup node_modules
is found in the current directory, and is always located under the root directory. That is, the first level to see if there is a node_modules
folder, if there is a corresponding module from the inside, if not found to go to the top level directory lookup.
Folder as a module
In addition to the node_modules
Next folder is a module, you can also place a folder in other places as a module, and then a relative or absolute path to the way to reference.
There are two ways (what the official web said three, in fact, two kinds) can make the folder as a module reference to.
- Folder to create a
package.json
file and use it "main": "abc.js"
in a way that require
will refer to the directory under the abc.js
file.
- When this file is not in the folder
package.json
, it tries to find index.js
or two files in the folder index.node
. If so, the corresponding file is used.
! Here is a question, what if there are folders and files of the same name at the same level? First, files and folders that use exactly the same name do not exist at the same level. Second, when similar to the existence of the hi.js
hi
directory at the same time? It will be the first to find the use hi
of the directory, if the directory does not find the results in the form above, then use hi.js
this file.
As many people (including myself) did not seriously read the introduction of the Modules module, so the node_modules
folder has a subconscious assumption that the node_modules
storage is the folder module, can not put a single JS file.
In fact, node_modules
folders, not only can be placed npm install
in the folder module, you can also place a single JS file to form a module.
If you put a test.js
file node_modules
in a folder, you can use it directly in your code require(‘test‘)
to reference it, just as you would reference a npm module.
The advantage of this is that you do not need to make a folder for a simple module, but you can also reference it without a path.
In fact, there are require
some necessary knowledge about node, such as the reference cache, the problem of referencing each other, which are explained in detail in the modules documentation, which is no longer translated.
[How the Require in Node.js]node is working