Skip to content Skip to sidebar Skip to footer

Does Require In Node Uses Eval To Run A Code In Another File

I was just trying to understand modules in node. I understood few things like node creates a module wrapper function around code placed in each file. Let's say there are 2 files, a

Solution 1:

More or less.

Node.js loads script file contents and wraps with module wrapper:

Module.wrap = function(script) {
  return Module.wrapper[0] + script + Module.wrapper[1];
};

Module.wrapper = [
  '(function (exports, require, module, __filename, __dirname) { ',
  '\n});'
];

Then module function is evaluated with vm.runInThisContext:

varwrapper=Module.wrap(content);varcompiledWrapper=vm.runInThisContext(wrapper, {
    filename:filename,
    lineOffset:0,
    displayErrors:true
  });

vm module provides V8 execution context, and vm.runInThisContext evaluates the code similarly to indirect eval:

vm.runInThisContext() compiles code, runs it within the context of the current global and returns the result. Running code does not have access to local scope, but does have access to the current global object.

<...>

Because vm.runInThisContext() does not have access to the local scope, localVar is unchanged. In contrast, eval() does have access to the local scope, so the value localVar is changed. In this way vm.runInThisContext() is much like an indirect eval() call, e.g. (0,eval)('code').

Solution 2:

When you do require then node.js have loader.js file which is responsible for loading the module. Following steps perform by loader.js file

  1. First, it checks in that module is present in the cached module or not. Checking is done by Module._cache
  2. if Not present in cached then, Creates a new Module instance.
  3. Saves into Cache
  4. Call module.load() with your the given filename. This will call module.compile() after reading the file contents.
  5. If Error in parsing the file (Because when compilation happen with any compiler there is the step of lexical analysis and parsing) then deletes the module from the cache
  6. Then returns module.export object.

As we know node is open source I am directly giving github path of loader.js of node.js

Path of loader.js

Here is the code of Module._load function which is written by Node.js Team

// Check the cache for the requested file.// 1. If a module already exists in the cache: return its exports object.// 2. If the module is native: call `NativeModule.require()` with the//    filename and return the result.// 3. Otherwise, create a new module for the file and save it to the cache.//    Then have it load  the file contents before returning its exports//    object.Module._load = function(request, parent, isMain) {
  if (parent) {
    debug('Module._load REQUEST %s parent: %s', request, parent.id);
  }

  var filename = Module._resolveFilename(request, parent, isMain);

  var cachedModule = Module._cache[filename];
  if (cachedModule) {
    updateChildren(parent, cachedModule, true);
    return cachedModule.exports;
  }

  if (NativeModule.nonInternalExists(filename)) {
    debug('load native module %s', request);
    returnNativeModule.require(filename);
  }

  // Don't call updateChildren(), Module constructor already does.varmodule = newModule(filename, parent);

  if (isMain) {
    process.mainModule = module;
    module.id = '.';
  }

  Module._cache[filename] = module;

  tryModuleLoad(module, filename);

  returnmodule.exports;
};

Also check this article to understand how process works

How require() Actually Works

Post a Comment for "Does Require In Node Uses Eval To Run A Code In Another File"