express-useragent is a robust and fast user-agent parsing library designed for Node.js environments, offering dedicated Express.js middleware and comprehensive TypeScript typings. Currently stable at version 2.1.0, the package underwent a significant rewrite in version 2.0.0, migrating to ES Modules and TypeScript, and now requires Node.js 18 or newer. Its release cadence appears active, with recent patches addressing bug fixes and dependency updates. Key differentiators include its first-class integration with Express, which populates `req.useragent` with parsed data, and its ability to parse user-agent strings directly via a `UserAgent` class instance. It also provides lightweight browser bundles for client-side parsing.
npm install express-useragentVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `express-useragent` middleware into an Express.js application to parse the user-agent string and expose the parsed data on `req.useragent` for easy access within route handlers.
For ESM, use `import { express as useragentMiddleware } from 'express-useragent'` for the middleware. CommonJS `require()` patterns remain largely compatible for the middleware via `require('express-useragent').express()`.Ensure your Node.js runtime environment is at least version 18.0.0.
Upgrade to version 2.1.0 or newer to ensure correct and consistent boolean output for `isBot`.
Use a named import for the middleware: `import { express as useragentMiddleware } from 'express-useragent'`, or use a namespace import: `import * as useragent from 'express-useragent'` and then `app.use(useragent.express())`.Correct the import statement for the middleware: `import { express as useragentMiddleware } from 'express-useragent'; app.use(useragentMiddleware());`Ensure your module system (ESM or CommonJS) matches your import/require statements. If in ESM, use `import` statements; if in CommonJS, use `require()` and ensure your project isn't configured as an ESM-only package.
Ensure you have `import 'express-useragent';` (or similar, depending on your project structure) in a global declaration file or an entry point. The package ships its types, so this usually indicates a setup issue.