Created
January 12, 2012 08:32
-
-
Save anatoliychakkaev/1599423 to your computer and use it in GitHub Desktop.
Require in runInNewContext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// put this file inside ./lib directory | |
require('./includeme'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// put this file inside ./lib directory | |
console.log('included'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~: ) 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A solution without vm (see nodejs/node-v0.x-archive#2525)
https://github.com/WolfgangKluge/node-helpers