{"id":18244,"library":"connect-inject","title":"connect-inject","description":"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.","status":"deprecated","version":"0.4.0","language":"javascript","source_language":"en","source_url":"git://github.com/danielhq/connect-inject","tags":["javascript","connect","inject"],"install":[{"cmd":"npm install connect-inject","lang":"bash","label":"npm"},{"cmd":"yarn add connect-inject","lang":"bash","label":"yarn"},{"cmd":"pnpm add connect-inject","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package is CommonJS only; no ESM wrapper. Using ES import will fail. Always use require.","wrong":"import inject from 'connect-inject';","symbol":"default","correct":"const inject = require('connect-inject');"},{"note":"Options object is passed directly to the factory function; no 'new' or constructor invocation.","wrong":"require('connect-inject')(new Object({snippet: '<script>...</script>'}));","symbol":"default (with options)","correct":"const middleware = require('connect-inject')({ snippet: '...' });"},{"note":"The factory must be called even without options; omit parentheses and the middleware won't be instantiated.","wrong":"app.use(require('connect-inject'));","symbol":"default (no options)","correct":"app.use(require('connect-inject')());"}],"quickstart":{"code":"const express = require('express');\nconst inject = require('connect-inject');\nconst app = express();\n\napp.use(inject({\n  snippet: ['<script src=\"/analytics.js\"></script>', '<script>console.log(\"injected\")</script>']\n}));\n\napp.get('/', (req, res) => {\n  res.send('<html><body><h1>Hello</h1></body></html>');\n});\n\napp.listen(3000);","lang":"javascript","description":"Demonstrates basic use of connect-inject middleware with Express, injecting two script tags into HTML responses."},"warnings":[{"fix":"Migrate to actively maintained alternatives like connect-livereload (original) or inject-lr-script.","message":"Package is unmaintained since 2015; no security updates or compatibility fixes for newer Node versions.","severity":"deprecated","affected_versions":">=0.4.0"},{"fix":"Always call the function: app.use(require('connect-inject')({...})).","message":"Options object must be passed to the factory function call; passing the function itself to app.use will throw.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Override the ignore option: ignore: [] to disable filtering.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Avoid using for non-HTML endpoints or provide a custom html option that checks Content-Type header.","message":"The HTML detection function uses a regex that may misidentify non-HTML content as HTML, leading to unintended injection into JSON or other responses.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure rule matches are independent or order them appropriately (e.g., head before body).","message":"Multiple rules with runAll: true apply sequentially; if a rule's match is modified by a previous rule, later rules may fail to match.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add parentheses: require('connect-inject')()","cause":"Calling the require result directly without invoking the factory function export.","error":"TypeError: require('connect-inject') is not a function"},{"fix":"Pass an options object with a snippet property: require('connect-inject')({ snippet: '...' })","cause":"The snippet option is missing or not provided when the middleware is invoked.","error":"Cannot read property 'indexOf' of undefined"},{"fix":"Override ignore option: ignore: [] or remove unneeded entries.","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).","error":"Snippet not injected into HTML response"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}