Skip to content

Instantly share code, notes, and snippets.

@anatoliychakkaev
Created January 12, 2012 08:32
Show Gist options
  • Save anatoliychakkaev/1599423 to your computer and use it in GitHub Desktop.
Save anatoliychakkaev/1599423 to your computer and use it in GitHub Desktop.
Require in runInNewContext
var Module = require('module');
var vm = require('vm');
var path = require('path');
var filename = process.cwd() + '/lib/eval.js';
var mod = new Module(filename);
var context = {
module: mod,
__filename: filename,
__dirname: path.dirname(filename),
require: function (path) {
return mod.require(path);
}
};
console.log(context);
var code = require('fs').readFileSync(filename).toString();
var m = vm.createScript(code.toString('utf8'), filename);
m.runInNewContext(context);
// put this file inside ./lib directory
require('./includeme');
// put this file inside ./lib directory
console.log('included');
~: ) NODE_DEBUG=module node demo
looking for "/Users/anatoliy/projects/sandbox/demo" in ["/usr/local/lib/node_modules","/Users/anatoliy/.node_modules","/Users/anatoliy/.node_libraries","/usr/local/Cellar/node/0.6.0/lib/node"]
load "/Users/anatoliy/projects/sandbox/demo.js" for module "."
Module._load REQUEST module parent: .
load native module module
Module._load REQUEST vm parent: .
load native module vm
Module._load REQUEST path parent: .
load native module path
{ module:
{ id: '/Users/anatoliy/projects/sandbox/lib/eval.js',
exports: {},
parent: undefined,
filename: null,
loaded: false,
exited: false,
children: [] },
__filename: '/Users/anatoliy/projects/sandbox/lib/eval.js',
__dirname: '/Users/anatoliy/projects/sandbox/lib',
require: [Function] }
Module._load REQUEST fs parent: .
load native module fs
Module._load REQUEST ./includeme parent: /Users/anatoliy/projects/sandbox/lib/eval.js
looking for "./includeme" in ["/Users/anatoliy/projects/sandbox/node_modules","/Users/anatoliy/projects/node_modules","/Users/anatoliy/node_modules","/Users/node_modules","/node_modules",".","/usr/local/lib/node_modules","/Users/anatoliy/.node_modules","/Users/anatoliy/.node_libraries","/usr/local/Cellar/node/0.6.0/lib/node"]
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module './includeme'
at Function._resolveFilename (module.js:334:11)
at Function._load (module.js:279:25)
at Module.require (module.js:357:17)
at /Users/anatoliy/projects/sandbox/run.js:13:20
at /Users/anatoliy/projects/sandbox/lib/eval.js:1:1
at Object.<anonymous> (/Users/anatoliy/projects/sandbox/run.js:21:3)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
@WolfgangKluge
Copy link

A solution without vm (see nodejs/node-v0.x-archive#2525)

// replacement for demo.js
var path = require('path');
var filename = path.join(process.cwd(), '/lib/eval.js');
var code = require('fs').readFileSync(filename).toString();

requireCode(code, filename);


function requireCode(code, pathToCode) {
    var vm = require("vm");
    var path = require("path");
    var Module = require("module").Module;

    var filepath = path.resolve(process.cwd(), pathToCode);
    var filename = path.basename(filepath);
    var dirname = path.dirname(filepath);

    var cachedModule = Module._cache[filepath];
    if (cachedModule) {
        return cachedModule.exports;
    }

    var mod = new Module(filepath, module);
    Module._cache[filepath] = mod;

    mod.filename = filepath;
    mod.paths = Module._nodeModulePaths(dirname);

    mod._compile(code, filepath);
    mod.loaded = true;

    return mod.exports;
}

https://github.com/WolfgangKluge/node-helpers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment