http-modules
v0.0.1
Published
Import HTTPS modules in Bun and Node.js.
Readme
http-modules
http-modules lets JavaScript runtimes import modules directly from https:// URLs.
The package currently supports:
- Bun, through a preloadable
Bun.plugin. - Node.js, through module customization hooks.
- Remote JavaScript and TypeScript modules.
- Relative imports inside fetched remote modules.
Why this exists
Browsers and Deno can import URL-based modules as first-class module specifiers. Bun and Node.js normally expect package, file, or built-in specifiers. This package fills that gap for runtime code that wants to write normal static imports against HTTPS module URLs:
import { nanoid } from "https://cdn.jsdelivr.net/npm/[email protected]/index.browser.js";Install
bun add http-modulesBun
Preload the Bun plugin before any static HTTPS imports are resolved:
# bunfig.toml
preload = ["http-modules/bun"]Then import HTTPS modules directly:
import { nanoid } from "https://cdn.jsdelivr.net/npm/[email protected]/index.browser.js";
console.log(nanoid());For local development in this repo, bunfig.toml preloads ./src/bun.ts so the source plugin is exercised directly.
Node.js
Preload the Node registration entrypoint:
node --import http-modules/node/register ./app.mjsThen import HTTPS modules directly:
import { basename } from "https://deno.land/[email protected]/path/mod.ts";
console.log(basename("/tmp/example.ts"));Node.js support uses asynchronous module customization hooks. Remote TypeScript modules require Node.js >=22.6, because Node must understand the module-typescript loader format.
If you need to register hooks yourself, use:
import { register } from "node:module";
register("http-modules/node/hooks", import.meta.url);Package entrypoints
| Export | Purpose |
| --- | --- |
| http-modules | Shared helper exports. No runtime side effects. |
| http-modules/bun | Registers the Bun HTTPS import plugin. Use as a Bun preload. |
| http-modules/node/register | Registers Node customization hooks. Use with node --import. |
| http-modules/node/hooks | Raw Node hook module for manual registration. |
Behavior
- Only
https://module specifiers are handled. - HTTP errors fail module loading with the response status.
- Remote JavaScript and TypeScript sources have relative import specifiers rewritten to absolute HTTPS URLs before evaluation. This keeps nested remote imports working across both runtimes.
- JSON modules are loaded as JSON where the runtime supports that loader format.
- No cache is implemented yet. Every process fetches remote modules through the runtime loader path.
Development
This repo uses Bun for package management and build tasks, with mise pinning Bun and Node versions.
mise install
bun install
bun run build
bun run test
bun run typecheckEquivalent mise tasks:
mise run build
mise run test
mise run typecheckbun run test builds dist/ and then runs the cross-runtime test suite. The tests start a local HTTPS server with fixture modules, then verify both Bun and Node can import the same remote module graph without depending on third-party networks.
Repository layout
src/shared.ts Shared fetch, format detection, and import-rewrite logic.
src/bun.ts Bun plugin entrypoint.
src/node/hooks.ts Node async resolve/load hooks.
src/node/register.ts Node --import registration entrypoint.
src/index.ts Side-effect-free public helper exports.Build output is written to dist/ and included in the published package.
