Check which JavaScript environment your code is running in at runtime
npm install environment
import {isBrowser, isNode} from 'environment';
if (isBrowser) {
console.log('Running in a browser!');
}
if (isNode) {
console.log('Running in Node.js!');
}
Note
Runtime checks should be used sparingly. Prefer conditional package exports and imports whenever possible.
Check if the code is running in a web browser environment.
Check if the code is running in a Node.js environment.
Check if the code is running in a Bun environment.
Check if the code is running in a Deno environment.
Check if the code is running in an Electron environment.
Check if the code is running in a jsdom environment.
Check if the code is running in a Web Worker environment, which could be either a dedicated worker, shared worker, or service worker.
Check if the code is running in a Dedicated Worker environment.
Check if the code is running in a Shared Worker environment.
Check if the code is running in a Service Worker environment.
Check if the code is running on macOS.
Check if the code is running on Windows.
Check if the code is running on Linux.
Check if the code is running on iOS.
Check if the code is running on Android.