Browserify Development Middleware
raw JSON →Browserify Dev Middleware provides an Express/Connect-compatible middleware for Node.js environments, designed to compile Browserify files on request specifically for development purposes. It enables dynamic bundling of client-side JavaScript, supporting features like specifying a source directory for assets, applying Browserify transforms (e.g., `jadeify`), global transforms (e.g., `deamdify`), and passing arbitrary Browserify options directly. This approach allows developers to avoid pre-compilation steps during local development, serving fresh bundles dynamically. The package is currently at version 1.5.0. However, its GitHub repository was archived in October 2021, indicating it is no longer actively maintained, making its release cadence effectively ceased. Its key differentiator was its simplicity in integrating on-demand Browserify compilation into existing Node.js servers, contrasting with more complex static asset pipelines.
Common errors
error TypeError: app.use is not a function ↓
const app = express(); or const app = connect();) and that app.use is called on that instance. error SyntaxError: Cannot use import statement outside a module ↓
require(): const browserifyDevMiddleware = require('browserify-dev-middleware');. Warnings
breaking The `browserify-dev-middleware` GitHub repository was archived in October 2021. This package is no longer actively maintained, meaning there will be no further bug fixes, security patches, or feature updates. Users should consider migrating to alternative solutions for long-term projects. ↓
gotcha This package is built for CommonJS (CJS) environments and uses `require()` for module loading. It does not natively support ES Modules (`import`/`export`) syntax. ↓
gotcha The package's Node.js engine requirement is `>= 4.0`. While it might function on newer Node.js versions, it has not been tested or updated for compatibility with recent Node.js releases, potentially leading to unforeseen issues, especially with breaking changes in Node.js APIs or internal modules. ↓
Install
npm install browserify-dev-middleware yarn add browserify-dev-middleware pnpm add browserify-dev-middleware Imports
- browserifyDevMiddleware wrong
import browserifyDevMiddleware from 'browserify-dev-middleware';correctconst browserifyDevMiddleware = require('browserify-dev-middleware');
Quickstart
const express = require('express');
const browserifyDevMiddleware = require('browserify-dev-middleware');
const path = require('path');
const app = express();
const assetsPath = path.join(__dirname, 'assets');
// Create a dummy asset file for demonstration
const fs = require('fs');
if (!fs.existsSync(assetsPath)) {
fs.mkdirSync(assetsPath);
}
fs.writeFileSync(path.join(assetsPath, 'commit.js'), 'module.exports = { message: "Hello from browserify!" };');
app.use(browserifyDevMiddleware({
src: assetsPath,
// Example of adding a transform, typically 'jadeify' or similar
// For this quickstart, we'll just demonstrate general options.
// transforms: [require('some-browserify-transform')]
noParse: [],
insertGlobals: true
}));
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>Browserify Dev Middleware Example</title>
</head>
<body>
<h1>Check console for bundled output</h1>
<script src="/commit.js"></script>
<script>
// commit.js will be served at /commit.js thanks to the middleware
fetch('/commit.js')
.then(response => response.text())
.then(text => console.log('Bundle content:', text))
.catch(error => console.error('Error fetching bundle:', error));
// You can also try to access window.module.exports directly if the bundle exposes it.
// This depends on browserify's output format.
</script>
</body>
</html>
`);
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
console.log(`Access /commit.js in your browser to see the bundled file.`);
});