Registry / web-framework / is-browser

is-browser

JSON →
library2.1.0jsnpmunverified

The `is-browser` package is a minimalist utility designed to determine if the current JavaScript execution environment is a web browser. It exports a single boolean value: `true` if executed in a browser context (detecting `window` and `document` globals), and `false` otherwise (typically in a Node.js environment). The package is at version `2.1.0` and has not received updates since 2018, indicating a mature, stable, but currently unmaintained codebase. Its primary use case is for simple conditional logic in universal JavaScript libraries that need to adapt behavior based on the runtime environment. Unlike more modern alternatives like `browser-or-node` (which offers checks for Web Workers, JSDOM, or Deno), `is-browser` provides only a basic browser vs. non-browser distinction. It has no external runtime dependencies, ensuring a minimal footprint.

npm install is-browser
INSTALL
IMPORT
SIG · IS-BROWSER
I
is-browser
web-frameworkjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

isBrowser
import isBrowser from 'is-browser';
import { isBrowser } from 'is-browser';
The package exports the boolean value directly as its default export, not as a named export. Avoid destructuring.
isBrowser (CommonJS)
const isBrowser = require('is-browser');
const { isBrowser } = require('is-browser');
In CommonJS, `require('is-browser')` directly resolves to the exported boolean value, not an object containing it.
Direct use
if (require('is-browser')) { /* ... */ }
if (require('is-browser')()) { /* ... */ }
The export is a boolean, not a function. It should be used directly in conditional statements.

Demonstrates how to use `is-browser` to conditionally execute code based on the runtime environment (browser vs. Node.js).

import isBrowser from 'is-browser'; function setupEnvironmentSpecificFeatures() { if (isBrowser) { console.log('Running in a browser environment. Initializing browser-specific features...'); document.addEventListener('DOMContentLoaded', () => { console.log('DOM is ready. Attaching UI event listeners.'); // Example: Initialize a client-side analytics library // window.myAnalytics.init(process.env.ANALYTICS_KEY ?? ''); }); } else { console.log('Running in a Node.js environment. Skipping browser-specific features.'); // Example: Connect to a database or perform server-side logging // console.log('Database connection string:', process.env.DATABASE_URL ?? ''); } } setupEnvironmentSpecificFeatures(); // To demonstrate how it works, you might run this in both browser and Node.js // In Node.js, `isBrowser` will be false. In a browser, it will be true.
Debug
Known issues
gotchaThe package has not been updated since 2018, meaning its environment detection logic may not account for newer JavaScript runtimes, environments (like Deno, Bun, or specific JSDOM setups), or evolving browser global properties. It primarily relies on the presence of `window` and `document`.
fix
For more robust and up-to-date environment detection, consider alternatives like `browser-or-node` (version 3.0.0, last updated 2 years ago) which explicitly checks for Web Workers, Deno, and JSDOM.
affects: >=2.0.0
gotchaGiven its age and lack of maintenance, there is a risk of future compatibility issues with new JavaScript language features, module systems, or build tool optimizations. While simple, an unmaintained package can introduce subtle issues over time.
fix
Evaluate if the project's long-term stability requires a more actively maintained environment detection library. If strict evergreen compatibility is needed, migrating to a different package is advisable.
affects: >=2.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require('is-browser')` in an ECMAScript Module (ESM) context.
fix
When using ESM, use `import isBrowser from 'is-browser';` instead of `require`. Ensure your project is configured for ESM or transpiles CommonJS modules correctly.
TypeError: isBrowser is not a function
Mistakenly trying to invoke `isBrowser()` as a function, rather than using its direct boolean value.
fix
The `is-browser` package exports a boolean directly. Use it as a variable: `if (isBrowser) { /* ... */ }` or `console.log(isBrowser);`.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
is-browser — npm install is-browser · libregistry