Load node.js module from string in memory

How would I require() a file if I had the file's contents as a string in memory, without writing it out to disk? Here's an example:

// Load the file as a string
var strFileContents = fs.readFileSync( "./myUnalteredModule.js", 'utf8' );

// Do some stuff to the files contents
strFileContents[532] = '6';

// Load it as a node module (how would I do this?)
var loadedModule = require( doMagic(strFileContents) );

The question is already answered by Andrey, but I ran into a shortcoming that I had to solve and which might be of interest for others.

I wanted the module in the memorized string to be able to load other modules via 'require', but the module path was broken with the above solution (so e.g. needle wasn't found). I tried to find an elegant solution to maintain the paths, by using some existing function but I ended up with hard wiring the paths:

function requireFromString(src, filename) {
  var m = new module.constructor();
  m.paths = module.paths;
  m._compile(src, filename);
  return m.exports;
}

console.log(requireFromString("var needle = require('needle'); 
  [...]
  exports.myFunc = myFunc;"));

After that I was able to load all existing modules from the stringified module. Any comments or better solutions highly appreciated!

function requireFromString(src, filename) {
  var Module = module.constructor;
  var m = new Module();
  m._compile(src, filename);
  return m.exports;
}

console.log(requireFromString('module.exports = { test: 1}'));

look at _compile, _extensions and _load in module.js

Based on Andrey Sidorov & Domi nic solutions, I was saddened by the fact of not being able to require a stringified module then I suggest this version *.

Code:

void function () {
    var Module,
        path,
        cache,
        dirname,
        resolve,
        sep,
        parseFilename;

    Module = require('module');
    path = require('path');

    cache = Module._cache;
    resolveFilename = Module._resolveFilename;

    dirname = path.dirname;
    resolve = path.resolve;
    sep = path.sep;

    parseFilename = function parseFilename(request, parent) {
        var filename,
            lastChar,
            length;

        filename = resolve(dirname(parent.filename), request);
        length = filename.length;

        if (filename.lastIndexOf('.js') !== length - 3) {
            lastChar = filename.charAt(length - 1);

            if (lastChar === '.' || lastChar === ':') {
                lastChar = sep;
                filename += lastChar;
            }

            if (lastChar === sep) {
                filename += 'index';
            }

            filename += '.js';
        }

        return filename;
    };

    Module._resolveFilename = function _resolveFilename(request, parent) {
        var filename;

        filename = parseFilename(request, parent);

        if (cache.hasOwnProperty(filename)) {
            return filename;
        }

        return resolveFilename(request, parent);
    };

    Module._register = function register(id, parent, src) {
        var filename,
            module,
            code;

        filename = parseFilename(id, parent);

        module = new Module(id, parent);

        code = typeof src === 'function'
            ? 'void ' + src.toString() + '();'
            : src;

        module.filename = filename;
        module.paths = parent.paths;
        module._compile(code, id);
        module.loaded = true;

        cache[filename] = module;

        return module;
    };
}();

Usage:

void function () {
    var Module,
        parent,
        child;

    Module = require('module');

    parent = Module._register('parent', process.mainModule, '\
        module.exports = {\
            name: module.filename,\
            getChild: function getChild() {\
                return require(\'child\');\
            }\
        };\
    ');

    Module._register('child', parent, function () {
        module.exports = {
            name: module.filename,
            getParent: function getParent() {
                return module.parent.exports;
            }
        };
    });

    child = require('child');

    console.log(child === child.getParent().getChild());
}();

* as you can see, it contains a function formater which provides a way to create some modules from functions.

I think the better way to approach this would be to have a parameter that you could set afterwards...

such as inside the file: myUnalteredModule.js

exports.setChanges = function( args )...

Then you could do:

 var loadedModule = require( 'myUnalteredModule' );
loadedModule