0
我有這樣的代碼的NodeJS從VM獲得功能和正常使用
// hello.js
var hello = function() {
return 'hello world';
};
var helloDate = function() {
console.log(new Date());
return 'hello world';
};
裝有的NodeJS VM
//test/test_hello.js
var sinon = require('sinon');
var vm = require('vm');
var fs = require('fs');
var source = fs.readFileSync('./hello.js').toString();
var local = new vm.createContext({});
var script = new vm.Script(source);
script.runInContext(local);
var localModule = {};
for (var o in local) {
localModule[o] = local[o];
}
before(function() {
});
it('hello', function() {
local.hello();
});
it('helloDate', function() {
localModule.helloDate();
});
但我用localModule.helloDate()
我收到此錯誤信息
$ mocha
✓ hello
1) helloDate
1 passing (7ms)
1 failing
1) helloDate:
ReferenceError: console is not defined
at Object.helloDate (evalmachine.<anonymous>:6:3)
at Context.<anonymous> (test/test_hello.js:26:15)
如何在nodejs中「正常」使用helloDate
?就像使用模塊的功能一樣。
感謝,但我儘量讓對面,拿在'vm'定義的功能。 – JuanPablo