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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
executeMW
✓ import executeMW from 'execute-middleware';
✗ const executeMW = require('execute-middleware');
CommonJS `require` is shown in old examples, but ESM `import` is preferred for modern Node.js and browser environments.
serial
✓ import { serial } from 'execute-middleware';
✗ import executeMW from 'execute-middleware'; const serial = executeMW.serial;
For direct access to the `serial` function, named import is often cleaner. Assuming `serial` is also a named export.
concurrent
✓ import { concurrent } from 'execute-middleware';
✗ const { concurrent } = require('execute-middleware');
Similar to `serial`, `concurrent` can be imported directly if available as a named export, which is common for modular libraries.
Demonstrates serial execution of Express-style middleware, including nested arrays and error handling. It shows how `req`, `res`, and `next` are passed as arguments to `executeMW.serial` and how errors propagate, skipping normal middleware to reach an error handler.
import executeMW from 'execute-middleware';
// Mock Express-like objects for demonstration
const req = {
url: '/',
method: 'GET',
params: {},
query: {},
body: {},
data: {} // For middleware to attach data
};
const res = {
statusCode: 200,
_body: null,
_headers: {},
send: function(data) {
this._body = data;
console.log('Response sent:', data);
},
status: function(code) {
this.statusCode = code;
return this;
},
json: function(data) {
this.send(JSON.stringify(data));
},
set: function(name, value) {
this._headers[name.toLowerCase()] = value;
return this;
}
};
// The final `next` function for the entire chain
const finalNext = (err) => {
if (err) {
console.error('Final next caught an error:', err.message);
if (!res._body) { // Only send if no middleware already sent one
res.status(500).json({ error: err.message || 'Internal Server Error' });
}
} else {
console.log('All middleware in chain completed successfully.');
if (!res._body) { // If no middleware sent a response
res.status(200).send('No response sent by middleware, but chain completed.');
}
}
};
// Some middleware
const SOME_MIDDLEWARE = [
(req, res, next) => {
console.log('Middleware 1: Initializing request data.');
req.data.message = 'Hello from MW1';
next();
},
[ // Nested middleware array
(req, res, next) => {
console.log('Middleware 2.1: Processing:', req.data.message);
req.data.processed = true;
next();
},
(err, req, res, next) => { // This is an error handler, won't be called here in normal flow
console.log('Middleware 2.2 (Error Handler): This should be skipped in normal flow.');
next(err); // Pass error along if called
}
],
(req, res, next) => {
console.log('Middleware 3: Final step, received:', req.data);
// Simulate sending a response
res.status(200).json({ status: 'success', data: req.data });
// Do not call next() here if response is sent, typically ends the chain
}
];
async function runSerialExample() {
console.log('--- Running Serial Middleware Example (Normal Flow) ---');
// Pass in the middleware array, then the usual express (req, res, next) values
await executeMW.serial(SOME_MIDDLEWARE, req, res, finalNext);
console.log('--- Serial Middleware Finished (Normal Flow) ---\n');
// Example with error propagation
const errorReq = { ...req, data: {} }; // Fresh request object
const errorRes = { ...res, _body: null }; // Fresh response object
const ERROR_MIDDLEWARE = [
(req, res, next) => {
console.log('Error MW 1: About to trigger an error.');
// Simulate an asynchronous operation that throws an error
setTimeout(() => {
next(new Error('Validation failed for input!'));
}, 50);
},
(req, res, next) => {
console.log('Error MW 2: This middleware should be skipped because of the error.');
next(); // This next() will not be called as error handler takes over
},
(err, req, res, next) => { // This is an error handler middleware
console.error('Error MW 3 (Error Handler): Caught error:', err.message);
errorRes.status(400).json({ message: 'Request Error', detail: err.message });
next(); // Call next to complete the error handling chain, allowing finalNext to run
}
];
console.log('--- Running Serial Middleware with Error Example ---');
await executeMW.serial(ERROR_MIDDLEWARE, errorReq, errorRes, finalNext);
console.log('--- Serial Middleware Finished (Error Flow) ---');
}
runSerialExample();
Errors
Common errors & fixes
TypeError: (intermediate value).serial is not a function
Attempting to call `.serial` or `.concurrent` on an `executeMW` object that was not correctly imported or is `undefined`. This often happens with incorrect CommonJS `require` syntax in an ESM project or vice-versa.
fixVerify your import statement. For CommonJS, use `const executeMW = require('execute-middleware');`. For ESM, use `import executeMW from 'execute-middleware';` or `import { serial } from 'execute-middleware';`. TypeError: next is not a function
The `next` argument was not provided when invoking `executeMW.serial` or `executeMW.concurrent`, or a non-function value was passed in its place. The `execute-middleware` library expects `req`, `res`, and `next` arguments after the middleware array.
fixEnsure you are passing valid `req`, `res`, and `next` mock objects/functions as the second, third, and fourth arguments respectively to `executeMW.serial()` or `executeMW.concurrent()`.
Audit
Dependencies
No dependency data recorded yet.