Does require in node uses eval to run a code in another file

Harish_N

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.js and b.js

In a.js, when i require b.js for first time, how will the module wrapper function which is present in b.js get's executed.

Does node do something like, get the whole content of b.js file as a string and then execute that from a.js using eval and then keep the result of this function call in cache.

Estus Flask

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:

  var wrapper = Module.wrap(content);

  var compiledWrapper = 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').

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

node cli - require file before running another

Node js require code instead of file

why node uses require not import?

Does Smartctl require a file system to run a test, and is my drive toast?

Why does require("the-module") from a file in another directory fail?

Why does this bash idiom require eval?

How to run code through eval?

Using eval() to run HTML code

mongo shell --eval does not run

Why does [eval] work in [eval][0]('code')?

php `require()` a file that has another `require()`

Why does AFrame require Node?

Should I do require('child_process').spawn('node', ['-e', code]) to run a lengthy operation?

Why does this code segfault on one machine but run fine on another?

JInt require another js file

Does Node run all the code inside required modules?

In Node, how do I require files inside eval?

Node javascript require js file?

Is it possible to run a Jetty server with a single connector using two ports, one that uses mutual authentication and another that does not?

Run ruby code with eval and it's gems, rails

Why does my vscode require me to write "python3" instead of just "python" to run a line of code

Mocha for node js app in Visual Studio 2019 is not showing Tests in Test Explorer when reference or require another file

Why does this code require casting?

Is there a way to read JS code as part of a JS file from another JS file ? (The app uses NodeJS)

Python twice opens a file if uses subprocess.Popen for run another script

Node CLI: eval file then eval command line argument

Does MySQL require a configuration file?

Visual Studio Code uses 'python' not 'py' to run Python files, which does nothing (3.7 64 bit in PATH)

Does require in javascript/nodejs executes same file everytime it is imported in another modules?