connect-inject
raw JSON → 0.4.0 verified Sat Apr 25 auth: no javascript deprecated
A Connect/Express middleware for injecting arbitrary script tags or content into HTML responses by intercepting res.write and res.end. v0.4.0 is the latest stable release; package appears unmaintained since 2015 (no recent commits, no changelog). Forked from connect-livereload, it adds support for multiple injection rules and array snippets. Unlike alternatives (e.g., inject-lr-script), it does not require a live-reload server and allows full snippet customization. Works with any Connect-compatible framework (Connect, Express, Grunt connect). Only notable differentiator is the `runAll` option for applying multiple injection rules sequentially.
Common errors
error TypeError: require('connect-inject') is not a function ↓
cause Calling the require result directly without invoking the factory function export.
fix
Add parentheses: require('connect-inject')()
error Cannot read property 'indexOf' of undefined ↓
cause The snippet option is missing or not provided when the middleware is invoked.
fix
Pass an options object with a snippet property: require('connect-inject')({ snippet: '...' })
error Snippet not injected into HTML response ↓
cause Response file extension is in the default ignore list (e.g., .html not ignored but .js, .css are; check if response has an extension that triggers ignore).
fix
Override ignore option: ignore: [] or remove unneeded entries.
Warnings
deprecated Package is unmaintained since 2015; no security updates or compatibility fixes for newer Node versions. ↓
fix Migrate to actively maintained alternatives like connect-livereload (original) or inject-lr-script.
gotcha Options object must be passed to the factory function call; passing the function itself to app.use will throw. ↓
fix Always call the function: app.use(require('connect-inject')({...})).
gotcha Ignore list defaults ignore .js, .css, .svg, etc. If your snippet injection isn't appearing, check that the response extension is not in the ignore list. ↓
fix Override the ignore option: ignore: [] to disable filtering.
gotcha The HTML detection function uses a regex that may misidentify non-HTML content as HTML, leading to unintended injection into JSON or other responses. ↓
fix Avoid using for non-HTML endpoints or provide a custom html option that checks Content-Type header.
gotcha Multiple rules with runAll: true apply sequentially; if a rule's match is modified by a previous rule, later rules may fail to match. ↓
fix Ensure rule matches are independent or order them appropriately (e.g., head before body).
Install
npm install connect-inject yarn add connect-inject pnpm add connect-inject Imports
- default wrong
import inject from 'connect-inject';correctconst inject = require('connect-inject'); - default (with options) wrong
require('connect-inject')(new Object({snippet: '<script>...</script>'}));correctconst middleware = require('connect-inject')({ snippet: '...' }); - default (no options) wrong
app.use(require('connect-inject'));correctapp.use(require('connect-inject')());
Quickstart
const express = require('express');
const inject = require('connect-inject');
const app = express();
app.use(inject({
snippet: ['<script src="/analytics.js"></script>', '<script>console.log("injected")</script>']
}));
app.get('/', (req, res) => {
res.send('<html><body><h1>Hello</h1></body></html>');
});
app.listen(3000);