It is the expected behaviour. Your code is evaluated in the same context, but not in the same scope, so module
, exports
and whatnot are undefined. You can do something like this:
var m = require('module')
var src = 'module.exports = 42'
var res = require('vm').runInThisContext(m.wrap(src))(exports, require, module, __filename, __dirname)
console.log(module.exports)
but there is no much point in doing that, because it's basically what require
does