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.
- is - Type check values