Skip to content
Node.js compatibility

Node.js compatibility

bunpy 0.8+ ships bunpy.node - a Go-backed shim of the Node.js standard library that lets Python code use the same APIs as Node.js scripts.

Available modules

ModuleNode.js equivalentKey exports
bunpy.node.fsfs / fs/promisesreadFile, writeFile, mkdir, readdir, stat, …
bunpy.node.pathpathjoin, resolve, dirname, basename, extname, …
bunpy.node.ososplatform, arch, hostname, homedir, tmpdir, cpus, …
bunpy.node.httphttpcreateServer, request, IncomingMessage, ServerResponse
bunpy.node.httpshttpscreateServer, request (TLS)
bunpy.node.netnetcreateServer, createConnection, Socket
bunpy.node.tlstlscreateServer, connect, TLSSocket
bunpy.node.cryptocryptorandomBytes, randomUUID, createHash, createHmac
bunpy.node.streamstreamReadable, Writable, PassThrough, Transform
bunpy.node.zlibzlibgzip, gunzip, deflate, inflate + Sync variants
bunpy.node.worker_threadsworker_threadsWorker, MessageChannel, isMainThread, threadId

Import pattern

from bunpy.node import fs, path, crypto

# or individual imports
from bunpy.node.crypto import randomUUID, createHash

Examples

fs

from bunpy.node import fs

content = fs.readFileSync("data.txt", "utf8")
fs.writeFileSync("out.txt", "hello world")

entries = fs.readdirSync(".")

crypto

from bunpy.node.crypto import createHash, randomUUID

digest = createHash("sha256").update("hello").digest("hex")
print(digest)  # 2cf24dba...

print(randomUUID())  # e.g. 550e8400-e29b-41d4-a716-446655440000

worker_threads

from bunpy.node.worker_threads import Worker, isMainThread, threadId

print(isMainThread)  # True
print(threadId)      # 0

w = Worker(lambda: print("running in worker"))
w.on("exit", lambda code: print(f"exited {code}"))

zlib

from bunpy.node import zlib

compressed = zlib.gzipSync(b"hello world")
original = zlib.gunzipSync(compressed)
print(original)  # b'hello world'

Top-level namespace

The bunpy.node module exposes all sub-modules as attributes:

import bunpy.node as node
node.fs.writeFileSync("x.txt", "hello")
node.path.join("/a", "b", "c")  # /a/b/c