'Difference between NodeJs preload option (-r) and explicit require in in REPL

I'm using esm which is a very useful package to support ES import keywords. According to the doc, in REPL, it can use preload command-line option to initialize it. i.e. node -r esm

Or explicitly initialize in the REPL:

require = require("esm")(module/*, options*/)
module.exports = require("./main.js")

Based on NodeJs help, -r is to preload a module with require. My question is why in REPL we can't just use: require("esm") to achieve the same function as preload option -r; instead, we have to override the default required variable in the context to make it work.

Tried to figure out the magic by looking at esm source code. But it's a bit too difficult for me to understand.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source