You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vm.runInThisContext() compiles code, runs it and returns the result. Running code does not have access to local scope. filename is optional, it's used only in stack traces.
but when you run a script that has invalid js that causes a syntax error...
var myFileName = "/path/to/some/file.js";
require("vm").runInThisContext('invalidJsHere ?= 1;', myFileName);
The output has no filename in it.
Here's sample output:
/Users/itaylor/temp/test.js:2
usingscript = vm.runInThisContext('localVar ?= 1;',
^
SyntaxError: Unexpected token =
at Object.<anonymous> (/Users/itaylor/temp/test.js:2:18)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)
Notice that the file name appears nowhere in the stack trace or the message. This makes it very difficult to track down errors in scripts that you are trying to run with vm, as the stack trace points you to the wrong place. Ideally, there'd be another element atop the stack trace with the line numbers of the script evaluated by vm.
Here's the output that would be ideal:
/path/to/some/file.js:2
localVar ?= 1;
^
SyntaxError: Unexpected token =
at <anonymous> (/path/to/some/file.js:2:9)
at vm.runInThisContext (vm.js:line:col)
at Object.<anonymous> (/Users/itaylor/temp/test.js:5:18)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Array.0 (module.js:484:10)
at EventEmitter._tickCallback (node.js:190:38)
From the vm docs:
but when you run a script that has invalid js that causes a syntax error...
The output has no filename in it.
Here's sample output:
Notice that the file name appears nowhere in the stack trace or the message. This makes it very difficult to track down errors in scripts that you are trying to run with vm, as the stack trace points you to the wrong place. Ideally, there'd be another element atop the stack trace with the line numbers of the script evaluated by vm.
Here's the output that would be ideal: