require is not defined when executing in vm

1,842 views
Skip to first unread message

zka

unread,
Feb 21, 2011, 9:32:39 AM2/21/11
to nodejs
Hello,

why don't we have access to require in vm module ?

here is the code:

var vm = require('vm');
var script = vm.createScript("var vm = require('vm');", 'sandbox.vm');
script.runInNewContext();

it throw a ReferenceError: require is not defined


mscdex

unread,
Feb 21, 2011, 5:51:31 PM2/21/11
to nodejs
IIRC the vm module supplies you with a v8 vm, not a node vm.

Isaac Schlueter

unread,
Feb 21, 2011, 6:37:59 PM2/21/11
to nod...@googlegroups.com
The purpose of the vm module is to create a new v8 virtual machine
that can be used for any purpose, especially sandboxing code. If
require() is exposed by default, then your sandbox has landmines.

If you want, you can expose stuff to the vm context like this:

var vm = require('vm')

var ret = vm.createScript('typeof require',
'sandbox.vm').runInNewContext({require:require})

> --
> You received this message because you are subscribed to the Google Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com.
> To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages