Node.js how does require work




















If we change the whole exports object, it would no longer be a reference to module. This is the way JavaScript reference objects work everywhere, not just in this context. There is nothing special about require. We can simply override the require object with our own logic if we want to. For example, maybe for testing purposes, we want every require call to be mocked by default and just return a fake object instead of the required module exports object.

This simple reassignment of require will do the trick:. After doing the above reassignment of require , every require 'something' call in the script will just return the mocked object. The require object also has properties of its own.

There is also require. Say, for example, that we have this simple printInFrame function in print-in-frame. The function takes a numeric argument size and a string argument header and it prints that header in a frame of stars controlled by the size we specify.

With require. Assuming the required module will export the printInFrame function and we can just call it:. Those are two different usages. We need a way to determine if the file is being run as a stand-alone script or if it is being required by other scripts.

So we can use this condition to satisfy the usage requirements above by invoking the printInFrame function differently:. When the file is not being required, we just call the printInFrame function with process. Otherwise, we just change the module.

Say that you have the following ascii-art. We want to display this header every time we require the file. So when we require the file twice, we want the header to show up twice. Node caches the first call and does not load the file on the second call.

We can see this cache by printing require. The cache registry is simply an object that has a property for every required module. Those properties values are the module objects used for each module. We can simply delete a property from that require. If we do that, Node will re-load the module to re-cache it. However, this is not the most efficient solution for this case. The simple solution is to wrap the log line in ascii-art. This way, when we require the ascii-art.

The Module type found in module. First, it provides a foundation for all Node. Each file is given a new instance of this base module on load, which persists even after the file has run. This is why we are able attach properties to module. The stand-alone require function that we use is actually an abstraction over module. Caching each module on load reduces the number of redundant file reads and can speed up your application significantly. In addition, sharing module instances allows for singleton-like modules that can keep state across a project.

This is why you use exports and module. Is that right? Please help me to understand require , or where can I find the source code. Source code is here. Your main script is wrapped before start in a function which has all the globals like require , process etc in its context.

Note that while module. Side effect of above: it's perfectly fine to have "return" statement in the middle of your module not belonging to any function , effectively "commenting out" rest of the code.

The require is a function that takes one argument called path, in this case the path is. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. How does require in node. Ask Question. Asked 9 years, 8 months ago. In our applications, we need to find the right balance of cohesion and coupling when creating modules. The desirable scenario is to achieve high cohesion and loose coupling of the modules. A module must be focused only on a single part of the functionality to have high cohesion. Loose coupling means that the modules should not have a global or shared state.

They should only communicate by passing parameters, and they are easily replaceable without touching your broader codebase. We usually export named functions or constants in the following way:. You can find out what version of npm you are using by executing:. It is also possible that the installation order changes your folder structure because npm 3 is non-deterministic in this manner. In this case, it installs your dependencies in alphabetical order, which also means that you will get the same folder tree.

This is important because the modules are cached using their path as the lookup key. There are two main ways for wiring modules. One of them is using hard coded dependencies, explicitly loading one module into another using a require call. The other method is to use a dependency injection pattern, where we pass the components as a parameter or we have a global container known as IoC, or Inversion of Control container , which centralizes the management of the modules.

We can allow Node. It organizes your packages in an intuitive way, which makes understanding and debugging easy. Dependency Injection is rarely used in a Node. The DI pattern can result in an improved decoupling of the modules. Instead of explicitly defining dependencies for a module, they are received from the outside. Therefore they can be easily replaced with modules having the same interfaces.



0コメント

  • 1000 / 1000