require
function to which you need to pass the path to the file. var authModule = require('./auth');
authModule
variable.require
function, the module connection algorithm will differ. So let's look at the principles of connecting modules in Node.js. I note that all this information is available in the documentation . var http = require('http'); var cluster = reqiure('cluster');
require
function. So for example, require('http')
will always return a built-in module, even if there is a third-party module with the same name or a file with that name. A list of all built-in modules and documentation on them can be found on the site . The sources of these modules can be viewed in the repository of the project .node_modules
folder. This folder contains all the modules that have been added using NPM . NPM is a package manager for Node.js, which makes it easy to find and connect third-party modules. At the time of this writing, it already contained 89,503 modules.node_modules
enough to indicate its name. var express = require('express'); var async = require('async');
'/var/www/demo'
folder, then Node.js will try to find the specified modules in the following folders:/ var / www / demo / node_modules / var / www / node_modules / var / node_modules / node_modules
node_modules
folder with the necessary module. NPM also makes it possible to install the module globally ( npm install -g MODULE
), then it will be available from any location.require
function begins with /
, ../
, or ./
, then the file for the connection will occur in the absolute path or relative to the current folder. var logger = require('../lib/logger'); var profiler = require('/var/lib/profiler');
.js
, .json
, and also .node
.require('./lib/users.js')
and require('./lib/users')
connect the same module.require
function.logger
is presented in the form of a folder with files:A─auth.js U─user.js Lo─logger Index─ index.js Console console.js Package─ package.json Config─ config.js D─ db.js
logger
module is that we simply pass the path to this folder: var require('./logger');
package.json
file will be checked or exists in the folder in which the name of the file to be connected is indicated in the main
field. { main: "./console.js" }
'./logger/console.js'
will be loaded. If there is no package.json
file, then Node.js will try to include the './logger/index.js'
or './logger/index.node'
. delete require.cache[ module ];
module
is the parameter you passed to the require
function to connect the module.require.resolve
function. var modulePath = require.resolve('express');
var package = require.resolve('express/package.json');
node_modules
folder with the desired module is located, you can use the fact that Node.js will go through all possible locations of the module.require
function, in addition to all this, has another useful property, main
. It stores the module that was launched from the command line. So from any module you can find out whether this module was launched directly or it was connected as a dependency of another module.require
function already discussed, a module object is available in each module
. The main objective of this object is to enable the module to return the result of its execution. It can be an object, a function, and a string — any data type.module
object has a property of exports
and it needs to assign everything that you want to return from the module. It is module.exports
will return as a result of the module connection. module.exports.createUser = function () { return new User(); }
exports
variable, which is essentially just a reference to module.exports
. exports.createUser = function () { return new User(); }
module.exports
object, so if the exports
variable is assigned a different value, it will no longer refer to module.exports
, which means the module will not return anything.module.exports
itself can be changed. Instead of an object, for example, you can return a function, and then the result of the module’s work will not be an object with a method in the form of a certain function, but a function itself.module
you can also find out or this module is launched from the command line. To do this, check the module.parent
property - it must be undefined
.Source: https://habr.com/ru/post/233827/
All Articles