2

I'd like to require a Node.js module from memory, e.g. because it is generated at runtime or received from a network stream.

Is this possible at all?

How could you resolve this issue (without writing to disk and loading from there, of course)?

Basically, something such as:

var code = 'module.exports = { foo: "bar" };';
var obj = require(code);
console.log(obj.foo); // => bar

The only approach I can currently think of is using the eval function, such as:

var myModule = {};
eval(
  '(function (module) {' +
  '  var local = 5;' +
  '  module.exports = {' +
  '    foo: \'bar\'' +
  '  };' +
  '  module.exports.baz = \'baz\';' +
  '})(myModule)');

After evaluating, myModule.exports contains the exported object:

{
  foo: 'bar',
  baz: 'baz'
}

and anything that should be local inside the module stays local.

Hence, you could create a function require2, such as:

var require2 = function (code) {
  var temp = {};
  eval('(function (module) {' + code + '})(temp);');
  return temp.exports;
};

Anyway, I am interested in better ideas ... :-)

PS: Being able to require from a stream would be awesome :-)

Golo Roden
  • 112,924
  • 78
  • 260
  • 376
  • 1
    The only sure way is to copy your module(s) to a [RAM Disk](http://www.quepublishing.com/articles/article.aspx?p=2008904) – paulsm4 Jan 16 '13 at 17:06
  • Which, in turn, I can not create from Node.js in an easy way, see http://stackoverflow.com/questions/14342024/how-do-i-create-a-ram-disk-using-node-js – Golo Roden Jan 16 '13 at 17:07
  • Have you considered doing something like saving the generated module as a temp file, clearing it and reloading as needed? – Morgan Jan 16 '13 at 17:25
  • Yes. This is possible, of course, but not very elegant as the file is not actually *needed*. Hence, if it could work without, I'd prefer it that way. – Golo Roden Jan 16 '13 at 17:25
  • Have you considered the security implications of this? Where are the modules coming from? – loganfsmyth Jan 16 '13 at 17:39
  • Yes, I have. They are from a trusted source (a certificate-secured, trusted https connection), and they are checked using an HMAC. In the end, this is not more or less secure than loading from the local file system. – Golo Roden Jan 16 '13 at 17:45
  • What are you gonna achieve with this? – Mustafa Jan 17 '13 at 21:24
  • Download modules on demand over the web using an ssl connection, verify their integrity using HMAC, and run them locally without the need to save them to disk. – Golo Roden Jan 17 '13 at 21:37
  • Maybe that the only way to get a module from a stream, could be write C++ module that provides that feature... – gsscoder Jan 19 '13 at 19:13

2 Answers2

0

In the absence of a better idea I now went with the approach I had already described in my question with a few tweaks, but the general idea is to do:

var require2 = function (code) {
  var temp = {};
  eval('(function (module) {' + code + '})(temp);');
  return temp.exports;
};
Golo Roden
  • 112,924
  • 78
  • 260
  • 376
0

This has already been answered here:

However, you can find a more complete implementation in the require-from-string package in Github.

The TL;DR is that you use the Modules package in node.js to _compile the code from a string.

Community
  • 1
  • 1
cshotton
  • 2,338
  • 2
  • 12
  • 8