I have a simple test case in mocha but it gives very different results:
const body: string = `
var moment = require('moment');
setInterval(function(){
console.log(moment().unix());
}, 2000)
`
const vm = new NodeVM({
console: 'redirect',
require: {
external: true,
root: process.cwd()
}
})
vm.run(body)
vm.on('console.log', function(consoleMessage){
console.log('from event', consoleMessage)
})
describe('Vanilla Script Test', function () {
it('should return a console log of a unix from moment', function (done) {
const body: string = `
var moment = require('moment');
setInterval(function(){
console.log(moment().unix());
}, 2000)
`
const vm = new NodeVM({
console: 'redirect',
require: {
external: true,
root: process.cwd()
}
})
vm.run(body)
vm.on('console.log', function(consoleMessage){
done()
})
})
})
VMError: You must specify script path to load relative modules.
If you pass a filename to vm.run
, like vm.run(body, 'myFile.js')
, it just works.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
If you pass a filename to
vm.run
, likevm.run(body, 'myFile.js')
, it just works.