I apologize for my English, but in the documentation for the node.js modules everything is written in such a way that I didn’t have enough with a translator:

It is a method for a module.

You must get a reference to the module object. The module.exports the module.exports, and it must be explicitly exported in order to be used.

    3 answers 3

    module.require and require are the same. The only difference is that module.require is a method, and require is a parameter :)


    In practice, the presence of the module.require method means that if you have a link to a certain module (which is not easy by itself, because the module is also a local parameter), you can call require as if it is executed from this very different module.

    The only module that is available out of the box is require.main , the main module (that is, the module that was launched). This allows you to do tricks like require.main.require('./config') , loading the config file not relative to the current module - but relative to the main one. Other modules, except the config, so it is better not to load, because each module should work no matter how and from where it was launched.


    The PS function, whose parameters are module and require , can be found in the documentation. This is a wrapper function over modules , inside of which each module is implicitly placed before it starts executing.

      It says that the module.require method provides the ability to load a module if the method is called from the main module. in this case, the method will return what is assigned to the variable module.exports inside the module.

      Suppose you have a module: myModule.js

       module.exports = { moduleFunction: function(arg) { console.log(arg); } } 

      and the main script index.js

       var myModule = require('./myModule'); myModule.moduleFunction('hello'); 

      this is how you load your module and call the moduleFunction method

      I want to note this is relevant not only for node.js but also for the usual web, for example with browserify

        It says that module.require allows you to load a module as if it were loaded from the parent module