Skip to content

Lesson 4 of 9 · 25m

npm Interop Is Compatibility, Not Identity

Use `npm:` specifiers and Node compatibility effectively while recognizing the module-resolution and lifecycle differences that break naive ports.

Deno's npm support is genuinely useful and dangerously easy to overstate. npm: means Deno can consume enormous parts of the JavaScript ecosystem. It does not mean Deno is Node wearing a different logo. The senior question is never "does the package import?" It is "which Node assumptions does this package secretly execute under load?"

The basic interop shape

import postgres from "npm:postgres@^3.4.5";
import { z } from "npm:zod@^3.24.0";

const sql = postgres(Deno.env.get("DATABASE_URL") ?? "");

Node built-ins are available through node: specifiers:

import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";

This works because Deno implements compatibility surfaces. Compatibility is a spectrum, not a binary state. APIs can exist and still differ in edge behavior, process lifecycle, stream backpressure, TLS defaults, or error shape.

Common failure modes

The first cluster is module-system archaeology:

dynamic require of generated path
package using require.resolve
conditional exports that select CommonJS internals
dependency expecting module.createRequire
postinstall scripts downloading native binaries

The second cluster is environmental assumption. Packages inspect process.env, mutate process.env, attach global handlers, assume __dirname, start their own listeners, or expect Node-specific signal behavior. Deno emulates much of this, but emulation is not proof of operational equivalence.

Test through the real execution path

Compatibility claims should be verified by running the actual service:

deno run --allow-net --allow-env=DATABASE_URL server.ts
deno test --allow-net=127.0.0.1:5432

A unit test that imports the package proves only that import resolution worked. Exercise connection pools, TLS negotiation, stream cancellation, worker behavior, process shutdown, and failure paths. These are where runtime differences become expensive.

Native addons deserve special caution

npm packages with N-API native addons can work when prebuilt binaries are available, but platform coverage, libc compatibility, and install scripts matter. A dependency that compiles on Node's supported targets may not support your container image or Deno version. If a native package is central, build a matrix before committing the migration.

Use npm interop as a bridge, then decide

Interop is strategically valuable because it lets you avoid rewriting mature libraries on day one. But decide which dependencies are permanent residents and which are temporary scaffolding. If a package drives the core request path, incompatibility is not a migration detail; it is a production risk.

Field notes

  • Pin npm versions with integrity locking; semver ranges without lock enforcement are not reproducible.
  • node: imports are compatibility surfaces, not proof every Node behavior is identical.
  • Postinstall scripts are supply-chain execution. Permissions do not automatically make dependency installation safe.
  • Test musl versus glibc containers when native modules are involved.
  • If a package monkeypatches globals, expect weird interactions with workers, tests, and hot reload.

Was this lesson helpful?

Sign in to rate