Sometimes, we want to load a Node.js module from string in memory.
In this article, we’ll look at how to load a Node.js module from string in memory.
How to load a Node.js module from string in memory?
To load a Node.js module from string in memory, we can use the require-from-string
module.
To install it, we run
npm install require-from-string
Then we use it by writing
const requireFromString = require('require-from-string');
const m = requireFromString('module.exports = 1');
to call requireFromString
with a string with the module’s code to require it.
As a result, m
is 1.
Conclusion
To load a Node.js module from string in memory, we can use the require-from-string
module.