Skip to main content
Documentation Status test

A Python 3 to Node.js + vm2 binding, helps you execute JavaScript safely.

vm2

vm2 is a node module to create real sandbox in node. The official node API vm, can only create isolate context and doesn’t prevent harmful code to damage your computer.

How it works

The module launchs a Node.js REPL server, which can be communicated with JSON. All JavaScript code are encoded in JSON and sent to the server. After the server executing the code in vm2, the result is sent back to Python.

Install

You need Node.js.

https://nodejs.org/

Install node_vm2 from pypi wheel.

pip install node_vm2

Also make sure you have node executable in PATH, or you can specify the executable with environment variable NODE_EXECUTABLE.

Additionally, you will need npm to build node_vm2 from source.

Usage

Most of the APIs are bound to vm2.

Simple eval:

from node_vm2 import eval

print(eval("['foo', 'bar'].join()"))

Use VM:

from node_vm2 import VM

with VM() as vm:
   vm.run("""
      var sum = 0, i;
      for (i = 0; i < 10; i++) sum += i;
   """)
   print(vm.run("sum"))

Use NodeVM:

from node_vm2 import NodeVM

js = """exports.greet = name => console.log(`Hello ${name}!`);"""

with NodeVM.code(js) as module:
   module.call_member("greet", "John")

It is possible to do async task with Promise:

from datetime import datetime
from node_vm2 import NodeVM

js = """
exports.test = () => {
   return new Promise(resolve => {
      setTimeout(() => {
         resolve("hello")
      }, 3000);
   });
};
"""
with NodeVM.code(js) as module:
   print(datetime.now())
   print(module.call_member("test"))
   print(datetime.now())

If you like to allow the VM to crash your server (e.g. process.exit()), you should create the VM in a separate server so it won’t affect other VMs:

from node_vm2 import VMServer, VM

with VMServer() as server:
   with VM(server=server) as vm:
      # now the vm is created in a new server
      print(vm.run("1 + 2 + 3"))

API reference

http://node-vm2.readthedocs.io/

Changelog

  • 0.4.6 (Oct 23, 2023)

    • Change: add deprecation warning.

    • Update vm2 to 3.9.19.

  • 0.4.5 (Sep 1, 2022)

    • Update vm2 to 3.9.11.

  • 0.4.4 (Mar 14, 2022)

    • Update vm2 to 3.9.9.

  • 0.4.3 (Feb 15, 2022)

    • Update vm2 to 3.9.7.

  • 0.4.2 (Feb 9, 2022)

    • Update vm2 to 3.9.6.

    • Fix: filename is optional.

  • 0.4.1 (Oct 20, 2021)

    • Update vm2 to 3.9.5.

  • 0.4.0 (Sep 2, 2021)

    • Update vm2 to 3.9.3.

    • Change: throw VMError when failed running node.

  • 0.3.7 (Mar 23, 2020)

    • Update vm2 to 3.9.0.

  • 0.3.6 (Apr 22, 2019)

    • Update vm2 to 3.8.0. Fix security issues.

  • 0.3.5 (Feb 10, 2019)

    • Update vm2 to 3.6.10. Fix security issues.

  • 0.3.4 (Aug 10, 2018)

    • Update vm2 to 3.6.3. Fix security issues.

  • 0.3.3 (Jul 23, 2018)

    • Fix: don’t bundle dev dependencies.

  • 0.3.2 (Jul 23, 2018)

    • Fix: getting a freezed object would crash the server.

    • Update vm2 to 3.6.2. Fix security issues.

  • 0.3.1 (Apr 25, 2017)

    • Add command arg to VMServer.

    • Fix: A dead default server is created if process spawning failed.

  • 0.3.0 (Apr 23, 2017)

    • Change: use event queue to handle console redirects.

    • Reconize object thrown by VM which doesn’t inherit built-in Error.

  • 0.2.0 (Mar 25, 2017)

    • Drop NodeBridge.

    • Add VMServer.

    • Make all VMs share a default VMServer.

    • Method rename: VM.connect -> VM.create, VM.close -> VM.destroy.

  • 0.1.0 (Mar 23, 2017)

    • First release

Release files for node-vm2 0.4.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for node-vm2 0.4.7
File Size Uploaded
node_vm2-0.4.7.tar.gz 217.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for node-vm2 0.4.7
File Interpreter ABI Platform
node_vm2-0.4.7-py3-none-any.whl Python 3 none any Details

Total release size: 450.5 kB

Release files / node_vm2-0.4.7.tar.gz

Download URL node_vm2-0.4.7.tar.gz
Size 217.7 kB
Tags Source
SHA-256 checksum
How to use checksums
2a5ffa77e6ddeff304fda0b3687bb59856b83ada5a3b7c57e42c579614c292d9
BLAKE2b-256 checksum
How to use checksums
e2e2e3abdf5542d4dd7472fddc7b817e3e6268138ccb0249edf358c59b09b709
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.11

Release files / node_vm2-0.4.7-py3-none-any.whl

Download URL node_vm2-0.4.7-py3-none-any.whl
Size 232.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b2e09319d5da4cb5846b4c065cb06cbfc941fcc237c80023c9c1a65d5e5d457
BLAKE2b-256 checksum
How to use checksums
31cfea851e4c8f3ddbeeb3401b67f7a485930a9f9433e9169d1c4237133deef4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.11

Release history Release notifications | RSS feed

This release

0.4.7 This release

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page