-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support for CommonJS module system #43
Comments
I tried a workaround using const app = express();
(async () => {
const requestId = (await import("express-request-id")).default;
console.log(requestId); // Output: [Function: requestID]
app.use(requestId());
})();
app.get("/", function (req, res, next) {
res.send(req.id || res.id || "No id"); // Result: both are undefined, I get 'No id'
}); This helped - https://adamcoster.com/blog/commonjs-and-esm-importexport-compatibility-examples#how-to-import-commonjs-cjs-into-esm |
Faced the same issue. Thankfully, the logic is quite small and contained, which allowed me to copy the code over to a local file and import it that way. import {v4 as uuidv4} from 'uuid';
import {Request, Response, NextFunction} from 'express';
function generateV4UUID(_request: any) {
return uuidv4();
}
const ATTRIBUTE_NAME = 'id';
export default function requestID({
generator = generateV4UUID,
headerName = 'X-Request-Id',
setHeader = true,
} = {}) {
return function (request: Request, response: Response, next: NextFunction) {
const oldValue = request.get(headerName);
const id = oldValue === undefined ? generator(request) : oldValue;
if (setHeader) {
response.set(headerName, id);
}
request[ATTRIBUTE_NAME] = id;
next();
};
} |
This was referenced Dec 28, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We have setup an express app with TypeScript, and target CommonJS modules (which is still the default in TypeScript). Consequenty, we don't have
"type": "module"
in package.json. When we install a version => 2.0.0 of express-request-id, and start the app, it crashes with the following error:A minimal repro using the latest version of
express-request-id
can be found here:https://github.com/HermannGruber/express-example
BTW, we tried to work around the issue by converting our app in full to ES2020 modules, but it seems that the rest of the ecosystem is not yet fully migrated to ESM. For example, jest 29.5 has only experimental support for ESM: https://jestjs.io/docs/ecmascript-modules
The text was updated successfully, but these errors were encountered: