Giter Club home page Giter Club logo

mimic-require's Introduction

MIMIC REQUIRE

require() function of Node.js is synchronous in nature. It returns module content using direct style.

This is a demonstration through a custom module system.

To run

node loader ./index.js

run the custom loader and provide it with a module name, as if required using require() function in Node.js

loader.js uses custom version of require() rather than the one provided by Node.js.


GLOSSARY

Revealing Module Pattern

Modules are the building blocks of an application or a package in Node.js. To avoid polluting the global scope of JS, modules are used. Everything inside a module is private unless it is assigned to module.exports variable. The technique here used is known as revealing module pattern.

var module = (function(){
  var privateVar1 = ...;
  var privateVar2 = function(){...};

  var export = {
    publicVar: function(){...};
  }
  return export;
});

Resolving Algorithm

NODEJS solves the problem of dependency hell(a scenario that arises when the dependencies of an application depend on a shared dependency but require different incompatible versions) by loading a different version of a module depending on where the module is loaded from.

This is due to Resolving Algorithm applied in the require() function. It is divided into:

  • File Modules: if moduleName starts with “/“, then its an absolute path to the module. If it starts with “./“ then moduleName is relative path(which is calculated from the requiring module).
  • Core Modules: if moduleName is not prefixed with “./“ or “/“, resolving algo will search within the Node.js Modules
  • Package Modules: if resolving algo cannot find matching moduleName in core modules then it searches inside node_modules directory. The resolving algo will continue to search until it reaches the root of the filesystem.

According to Resolving Algorithm, each package in node_modules will have their own private dependencies:

myAPP-root
|— app.js
|__ node_modules
	|— depA
	|	|— index.js
	|— depB
	|	|— index.js
	|	|__ node_moudles
	|	|__ depA
	|		|— index.js
	|__depC
		|—index.js
		|__node_modules
			|__depA
				|— index.js

The resolving algorithm is applied transparently when require() is invoked. However, if needed, it can still be used directly by any module by simply invoking require.resolve().


Module Caching

Each module is loaded and evaluated only the first time it is required.

Any following call of require() to that module will simply return the cached version.

Caching is crucial:

  • for performance
  • makes possible to have cycles(circular dependencies) within module dependencies

module cache is exposed in the require.cache variable.

mimic-require's People

Contributors

amandeepmittal avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.