Browserify Development Middleware

raw JSON →
1.5.0 verified Thu Apr 23 auth: no javascript abandoned

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.

error TypeError: app.use is not a function
cause The `browserify-dev-middleware` package returns a middleware function. This error typically occurs when attempting to use it with a framework that doesn't provide an `app.use` method, or if `app` is not correctly initialized as an Express/Connect application.
fix
Ensure you have an Express or Connect app instance (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
cause This error arises when trying to use ES module `import` syntax (`import browserifyDevMiddleware from 'browserify-dev-middleware';`) in a Node.js project configured for CommonJS, or in a script where `browserify-dev-middleware` expects CommonJS `require()`.
fix
Change your import statement to use CommonJS require(): const browserifyDevMiddleware = require('browserify-dev-middleware');.
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.
fix Evaluate modern asset bundling solutions like Webpack, Rollup, Vite, or a more actively maintained Browserify middleware (e.g., `browserify-middleware` by ForbesLindesay) for development environments.
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.
fix Ensure your Node.js project uses CommonJS modules (`.js` files with `require`/`module.exports`, or `type: "commonjs"` in `package.json`). If integrating with an ES Module project, a CJS wrapper or dynamic `import()` might be required, but it's generally advised to use an ESM-compatible bundler.
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.
fix Test thoroughly on your target Node.js version. For production or new projects, consider modern alternatives that are actively maintained and support current Node.js LTS releases.
npm install browserify-dev-middleware
yarn add browserify-dev-middleware
pnpm add browserify-dev-middleware

This quickstart demonstrates how to integrate `browserify-dev-middleware` with an Express application to serve a Browserify-bundled file on demand. It configures the middleware to watch a specified `assets` directory and serves `commit.js` dynamically, which can then be included in an HTML page.

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.`);
});