express-webpack-assets

raw JSON →
0.1.0 verified Sat Apr 25 auth: no javascript maintenance

Express middleware that loads hashed Webpack asset manifests generated by assets-webpack-plugin, exposing asset paths (JS, CSS, etc.) to templates via a `webpack_asset()` helper. Version 0.1.0 is the latest stable release. It supports single or multiple JSON manifest files (merged in filesystem order). Useful for referencing versioned assets without hardcoding hashes. Not actively maintained; alternatives include webpack-manifest-plugin with express middleware.

error Cannot read property 'js' of undefined
cause The asset key or file type does not exist in the loaded manifest JSON.
fix
Verify the manifest file contains the expected asset keys (e.g., 'one') and file types (e.g., 'js').
error Error: ENOENT: no such file or directory, open './config/webpack-assets.json'
cause The path provided to the middleware does not point to an existing file or directory.
fix
Ensure the path is correct relative to the project root or use an absolute path.
error webpack_asset is not defined
cause The middleware did not run before the template rendering, or the path is invalid causing the helper not to be injected.
fix
Ensure app.use(webpackAssets(...)) is placed before any route handlers that render templates.
error TypeError: webpackAssets is not a function
cause Importing the module incorrectly (e.g., using named import instead of default import).
fix
Use import webpackAssets from 'express-webpack-assets' or const webpackAssets = require('express-webpack-assets').default in CommonJS.
gotcha When passing a directory path, the order of merging JSON files is determined by filesystem read order (not alphabetical), which can cause unexpected overrides.
fix Use a single JSON file or ensure unique property names across files.
gotcha The `webpack_asset` helper is attached as a local variable to Express `res.locals`, so it is only available in templates that are rendered from the same response context.
fix Access the helper only within templates rendered after the middleware runs.
deprecated The package is no longer actively maintained; consider using webpack-manifest-plugin with a custom Express middleware.
fix Migrate to a more modern asset management solution.
npm install express-webpack-assets
yarn add express-webpack-assets
pnpm add express-webpack-assets

Sets up Express middleware to load a webpack assets JSON file and expose the `webpack_asset` helper in templates.

import express from 'express';
import webpackAssets from 'express-webpack-assets';

const app = express();
app.set('view engine', 'ejs');
// Load asset manifest and inject webpack_asset helper into res.locals
app.use(webpackAssets('./config/webpack-assets.json', { devMode: false }));

app.get('/', (req, res) => {
  // In the EJS template, use: <script src="<%= webpack_asset('one', 'js') %>"></script>
  res.render('index');
});

app.listen(3000);