Skip to content

Instantly share code, notes, and snippets.

@AnnaMag
Created December 28, 2016 20:48
Show Gist options
  • Save AnnaMag/6d79a9d493181e1942a1cbe7a2c3e9f5 to your computer and use it in GitHub Desktop.
Save AnnaMag/6d79a9d493181e1942a1cbe7a2c3e9f5 to your computer and use it in GitHub Desktop.
Node.js doc: Outside of scripts run by the vm module, "global variables" will remain unchanged.
const util = require('util');
const vm = require('vm');
var globalVar = 3;
const sandbox = { globalVar: 1 };
vm.createContext(sandbox);
vm.runInContext('globalVar *= 2;', sandbox);
console.log(util.inspect(sandbox)); // 2
console.log(util.inspect(globalVar)); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment