Koa Node Resolve Middleware

1.0.0-pre.9 · active · verified Tue Apr 21

This Koa middleware dynamically rewrites Node.js bare module specifiers (e.g., `import { foo } from "stuff";`) into relative paths (e.g., `./node_modules/stuff/index.js`) on the fly, directly within HTML and JavaScript files served by the Koa server. It's specifically designed to facilitate modern JavaScript module development in browsers without requiring a separate build step, by resolving module paths using the same rules as Node.js `require()`. The package is currently in a pre-release state (version `1.0.0-pre.9`), indicating ongoing active development towards a stable `1.0.0` release, though no specific release cadence is published. Its key differentiator lies in its real-time, on-demand transformation capabilities, making it highly suitable for rapid iteration in development servers, integrated testing environments (such as those using Karma), and simple static file serving. However, it explicitly carries a significant caveat: it is *not* recommended for high-volume production use due to the inherent performance overhead associated with parsing and transforming HTML and JavaScript content on every request. This focus on development efficiency over production performance is a core design principle.

Common errors

Warnings

Install

Imports

Quickstart

Sets up a basic Koa development server with `koa-node-resolve` to dynamically transform bare module specifiers in served HTML and JavaScript files to relative paths, making them runnable directly in browsers without a build step. It also includes `koa-static` for serving files.

npm install --save-dev koa koa-static koa-node-resolve

// dev-server.js
const Koa = require('koa');
const staticFiles = require('koa-static');
const { nodeResolve } = require('koa-node-resolve');

const server = new Koa()
  .use(nodeResolve({
    root: process.cwd(), // Important for module resolution
    logLevel: 'debug' // See all resolution details
  }))
  .use(staticFiles('.'))
  .listen(3000, () => {
    console.log('Development server running on http://localhost:3000');
    console.log('Access your HTML/JS files that use bare module specifiers.');
  });

// To run: node dev-server.js

view raw JSON →