Express User-Agent Parser
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.
Common errors
-
TypeError: useragent is not a function
cause In an ES Module project (`"type": "module"` in `package.json`), you are likely trying to import the default export as the middleware (`import useragent from 'express-useragent'`) and then calling `app.use(useragent())`.fixCorrect the import statement for the middleware: `import { express as useragentMiddleware } from 'express-useragent'; app.use(useragentMiddleware());` -
ERR_REQUIRE_ESM or ReferenceError: require is not defined
cause You are attempting to use CommonJS `require()` syntax in an ES Module context, or vice-versa, without proper configuration or loaders.fixEnsure 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. -
Property 'useragent' does not exist on type 'Request'. (TypeScript)
cause Your TypeScript environment is not correctly recognizing the augmentation of the `express.Request` interface by the middleware, or the types are not being picked up.fixEnsure 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.
Warnings
- breaking Version 2.0.0 introduced significant breaking changes, including a migration to ES Modules and TypeScript. The default export changed from the Express middleware factory to the `UserAgent` class instance.
- gotcha The package now requires Node.js 18 or newer due to its ES Module and TypeScript rewrite in v2.0.0. Older Node.js versions will likely encounter syntax or runtime errors.
- gotcha Prior to v2.1.0, the `isBot` property could exhibit inconsistent behavior, sometimes returning non-boolean values or producing false positives for certain user agents (e.g., TikTok).
- gotcha In ESM, attempting to use `import useragent from 'express-useragent'` and then `app.use(useragent())` will fail, as the default export is the `UserAgent` class (for direct parsing), not the middleware factory.
Install
-
npm install express-useragent -
yarn add express-useragent -
pnpm add express-useragent
Imports
- UserAgent
const { UserAgent } = require('express-useragent')import { UserAgent } from 'express-useragent' - express
import useragentMiddleware from 'express-useragent'
import { express as useragentMiddleware } from 'express-useragent' - useragent
import useragent from 'express-useragent'
const useragent = require('express-useragent')
Quickstart
import express from 'express';
import { express as useragent } from 'express-useragent';
const app = express();
// Apply the user-agent middleware
app.use(useragent());
// Define a route to display parsed user-agent data
app.get('/', (req, res) => {
if (!req.useragent) {
return res.status(500).json({ error: 'User-agent data not found' });
}
res.json({
browser: req.useragent.browser,
os: req.useragent.os,
isMobile: req.useragent.isMobile,
platform: req.useragent.platform,
source: req.useragent.source
});
});
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});