Registry / web-framework / connect-inject

connect-inject

JSON →
library0.4.0jsnpmunverified

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.

npm install connect-inject
INSTALL
IMPORT
SIG · CONNECT-INJECT
C
connect-inject
web-frameworkjavascriptv0.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const inject = require('connect-inject');
import inject from 'connect-inject';
Package is CommonJS only; no ESM wrapper. Using ES import will fail. Always use require.
default (with options)
const middleware = require('connect-inject')({ snippet: '...' });
require('connect-inject')(new Object({snippet: '<script>...</script>'}));
Options object is passed directly to the factory function; no 'new' or constructor invocation.
default (no options)
app.use(require('connect-inject')());
app.use(require('connect-inject'));
The factory must be called even without options; omit parentheses and the middleware won't be instantiated.

Demonstrates basic use of connect-inject middleware with Express, injecting two script tags into HTML responses.

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);
Debug
Known issues
deprecatedPackage 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.
affects: >=0.4.0
gotchaOptions 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')({...})).
affects: >=0.1.0
gotchaIgnore 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.
affects: >=0.1.0
gotchaThe 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.
affects: >=0.1.0
gotchaMultiple 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).
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: require('connect-inject') is not a function
Calling the require result directly without invoking the factory function export.
fix
Add parentheses: require('connect-inject')()
Cannot read property 'indexOf' of undefined
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: '...' })
Snippet not injected into HTML response
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.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
12
Amazon
3
OpenAI (training)
1
Resources
connect-inject — npm install connect-inject · libregistry