Universal User Agent String

7.0.3 · active · verified Sun Apr 19

universal-user-agent is a JavaScript library designed to retrieve a user agent string consistently across various runtime environments, including browsers and Node.js. It is currently stable at version 7.0.3 and maintains an irregular release cadence, primarily driven by bug fixes and necessary environment compatibility updates. Its key differentiator is its ability to abstract away environment-specific logic, providing a unified `getUserAgent` function. This makes it particularly useful for libraries and applications that need to report consistent user agent information regardless of where they are executed, for purposes such as API logging, telemetry, or feature detection.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `getUserAgent` to log the detected user agent string and infer the runtime environment (browser or Node.js).

import { getUserAgent } from 'universal-user-agent';

function logUserAgent() {
  const userAgent = getUserAgent();
  console.log('Detected User Agent:', userAgent);

  if (userAgent.includes('Node.js')) {
    console.log('Running in Node.js environment.');
    console.log('Node.js Version:', process.version);
  } else if (typeof navigator !== 'undefined' && navigator.userAgent) {
    console.log('Running in Browser environment.');
    console.log('Browser User Agent string:', navigator.userAgent);
  } else {
    console.log('Environment undetectable or highly minimalist.');
  }
}

logUserAgent();

view raw JSON →