Node.js AdvancedLesson 93Free preview
VM Module
This lesson is part of Node.js · All levels
Summary
This lesson discusses the built-in vm (virtual machine) module that evaluates JavaScript code in the context of a limited sandbox. You'll use vm.runInNewContext and vm.createContext to run third-party scripts with your own built-in global object. It's important to understand that vm is not a strong security sandbox against malicious code. (Node.js)
Key points
- Vm.runInNewContext(code, sandbox) executes a string of code with a separate global object.
- Vm.createContext(obj) transforms an object into a context that can be used repeatedly.
- The execution result is the last value of the expression evaluated.
- It's perfect for simple engine templates, formula calculators, or reliable plugins.
- Security warning: VM is NOT secure enough for untrusted code; use process/container isolation when necessary.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.