I was looking for a way to do some simple scripting in Node JS and discovered the VM module. The documentation specifies that the run* methods return the result of execution, so I thought "Hey, why not just return an object that way and then call on its properties in my main script?"
So I fire up a Node REPL:
$ node
> var vm = require('vm');
undefined
> vm.runInNewContext("{ foo: 'bar' }")
'bar'
> vm.runInNewContext("{ foo: 'bar', baz: 'qux' }")
evalmachine.<anonymous>:1
{ foo: 'bar', baz: 'qux' }
^
SyntaxError: Unexpected token :
>
Not quite the expected results. Interestingly, if I return the result of assignment...
> vm.runInNewContext("exports = { foo: 'bar', baz: 'qux' }")
{ foo: 'bar', baz: 'qux' }
Can someone explain this behavior to me?