Load node.js module from string in memory

The require-from-string package does the job.

Usage:

var requireFromString = require('require-from-string');

requireFromString('module.exports = 1');
//=> 1

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;
}

var codeString = 'var needle = require(\'needle\');\n'
  + '[...]\n'
  + 'exports.myFunc = myFunc;';

var virtMod = requireFromString(codeString);
console.log('Available public functions: '+Object.keys(virtMod));

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


After analyzing the source code of such solutions as pirates and require-from-string, I came to the conclusion that a simple mock of fs and Module methods would be no worse in terms of support. And in terms of functionality it will be better, because it supports @babel/register, pirates and other modules that changes the module loading process.

You can try this npm module require-from-memory

import fs from 'fs'
import BuiltinModule from 'module'
const Module = module.constructor.length > 1 ? module.constructor : BuiltinModule

function requireFromString(code, filename) {
    if (!filename) {
        filename = ''
    }

    if (typeof filename !== 'string') {
        throw new Error(`filename must be a string: ${filename}`)
    }

    let buffer
    function getBuffer() {
        if (!buffer) {
            buffer = Buffer.from(code, 'utf8')
        }
        return buffer
    }

    const now = new Date()
    const nowMs = now.getTime()
    const size = Buffer.byteLength(code, 'utf8')
    const fileStat = {
        size,
        blksize    : 4096,
        blocks     : Math.ceil(size / 4096),
        atimeMs    : nowMs,
        mtimeMs    : nowMs,
        ctimeMs    : nowMs,
        birthtimeMs: nowMs,
        atime      : now,
        mtime      : now,
        ctime      : now,
        birthtime  : now
    }

    const resolveFilename = Module._resolveFilename
    const readFileSync = fs.readFileSync
    const statSync = fs.statSync
    try {
        Module._resolveFilename = () => {
            Module._resolveFilename = resolveFilename
            return filename
        }

        fs.readFileSync = (fname, options, ...other) => {
            if (fname === filename) {
                console.log(code)
                return typeof options === 'string'
                    ? code
                    : getBuffer()
            }
            console.log(code)
            return readFileSync.apply(fs, [fname, options, ...other])
        }

        fs.statSync = (fname, ...other) => {
            if (fname === filename) {
                return fileStat
            }
            return statSync.apply(fs, [fname, ...other])
        }

        return require(filename)
    } finally {
        Module._resolveFilename = resolveFilename
        fs.readFileSync = readFileSync
        fs.statSync = statSync
    }
}

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