Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Neon 6 min read How-to mediumAdept

How to use NPM packages outside of Node

Many npm packages depend on Node-only globals like Buffer or built-ins like fs and net, which break on Vercel Edge Functions, Cloudflare Workers and browsers. Using esbuild bundling, the techniques progress from simplest to hardest: install third-party shims, inject global shims with --inject (with checks for booby-trapped Buffer and process.nextTick), add stub packages as local file: dependencies, reimplement small modules like StringDecoder and url.parse, and reimplement complex ones such as net.Socket over WebSockets. A final trick, package masquerading, substitutes a dependency (pg) with another (@neondatabase/serverless) via a same-named local package. The examples come from building Neon's serverless Postgres driver from node-postgres.

Assumes: npm package.json dependenciesJavaScript module bundlingNode.js built-in modules

Related posts

  • Edge Functions: Node and native npm compatibility

    Supabase mediumAdept

    Supabase Edge Functions now natively support npm modules (via `npm:` imports) and Node built-in APIs, so existing Node apps using Express, node-postgres and Drizzle can migrate with minimal changes. Under the hood, the open-source Deno-based Supabase Edge Runtime serializes each function's module graph into a single eszip at deploy time and loads all modules from it in the hosted, local and self-hosted environments alike. This avoids fetch latency, dependency conflicts with locally installed packages, and bugs such as failures when a deno.lock file is present. The release also adds regional invocation via an `x-region` header, CPU/memory and status-code metrics in the dashboard, and error tracking through Sentry's Deno SDK.

  • Bun v0.5

    Bun mediumAdept

    Bun v0.5 adds npm workspaces support (installing the Remix monorepo in about 500ms, claimed 28x faster than npm), a Bun.dns lookup API backed by c-ares, node:readline, and socket support via node:net and node:tls that lets database drivers like Postgres.js, mysql2 and node-redis run for the first time. Performance changes include 10x faster Buffer instantiation by removing Object.setPrototypeOf from construction and 50x faster crypto.createHash by switching from pure JS to BoringSSL. Module resolution now ignores the package.json browser field and honors the node export condition, and the release adds HTTPS_PROXY support, custom WebSocket handshake headers, more Jest-compatible test APIs, and many bun install fixes.

  • Bun 1.2

    Bun mediumAdept

    Bun 1.2 makes a major push on Node.js compatibility by porting thousands of files from the Node.js test suite into Bun's CI, with stubbed internal bindings and assertions on error name/code rather than message text. It adds a node:http2 server, node:dgram, node:cluster, a native rewrite of node:zlib with Brotli, V8 C++ addon API support implemented on JavaScriptCore, and V8 heap snapshots. It also introduces a native S3 client (Bun.s3) with presigned URLs, multipart writers and s3:// support in Bun.file and fetch, a built-in Postgres client (Bun.sql), and a text-based bun.lock lockfile. Claimed numbers include Express up to 3x faster than Node.js, HTTP/2 serving 2x faster, and S3 downloads 5x faster than @aws-sdk/client-s3.

  • The Bun Bundler

    Bun mediumAdept

    Bun now ships a native bundler in beta, exposed as the `bun build` CLI and the `Bun.build()` JavaScript API. It shares one plugin API with the runtime, returns Blob-compatible build artifacts, can compile standalone executables, and supports browser, bun and node targets. Bundles built for the bun target carry a `// @bun` pragma so the runtime skips re-transpilation. It includes tree shaking (sideEffects, __PURE__, NODE_ENV/--define, ESM and limited CommonJS-to-ESM conversion), source maps and a new minifier, and it benchmarks at 1.75x faster than esbuild and 220x faster than Webpack on a three.js workload. Only ESM output is supported for now, and its architecture follows esbuild's design.

  • Bun v0.6.7

    Bun mediumAdept

    Bun v0.6.7 fixes Node.js compatibility bugs in Node-API, node:http, undici, WebSocket, node:net and CommonJS/ESM interop, which lets Prisma, Discord.js and Puppeteer run in Bun. It also roughly doubles node:crypto hashing speed on small inputs and fixes regressions from the native CommonJS rewrite, including a scope-lifetime crash and incorrect __dirname/__filename injection. Two known CommonJS differences from Node.js remain: module evaluation order, and Bun's runtime-based detection of named exports instead of static analysis. Bundler fixes cover __esModule handling, jsx vs jsxDEV selection, bun build --compile and JSON default imports.

The quest for software engineering knowledge.

Look:
Layout: