I have a silly project on GitHub right now called 101 Hello Worlds. The goal is to come up with 101 unique and convoluted ways to write a function that returns a simple string: Hello, World. It's fun, in the spirit of code golfing and requires you to think outside the box.
Requiring Modules Inside Evals Invoked by Reference
require statement it will only work if
eval is called directly and will fail if it's invoked by reference.
What does that mean in plain English? It's probably easier to show in code.
This should work:
This should not work:
const e = eval; e("require('./module.js')")
You will get the error message
require is not defined, which is probably a first if you were like us.
Why is that? The description over at Mozilla Developer Network says evals invoked by reference will work in the "global" scope instead of the "local" scope. I guess that makes
require unavailable to it in this context, for whatever reason.
Adding to the confusion, when I try these exact same examples from the Node.js REPL both approaches work for me.
Obligatory warning: Never use eval()!.
Tagged Template Strings
Not to be confused with your run-of-the-mill template literals, which I use quite often and find very useful.
Did you know you can do this?
That's not a typo. You're probably used to seeing it formatted more like this:
Wes Bos has a nice explanation about tagged template strings that actually makes sense and even provides a use-case for why you might want them.