
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
browser-or-node
Advanced tools
The 'browser-or-node' npm package is a utility that helps developers determine whether their code is running in a browser or Node.js environment. This can be particularly useful for writing isomorphic JavaScript that needs to behave differently depending on the runtime environment.
Detect if running in a browser
This feature allows you to check if the code is being executed in a browser environment. The 'isBrowser' boolean will be true if the code is running in a browser.
const { isBrowser } = require('browser-or-node');
if (isBrowser) {
console.log('Running in a browser');
}
Detect if running in Node.js
This feature allows you to check if the code is being executed in a Node.js environment. The 'isNode' boolean will be true if the code is running in Node.js.
const { isNode } = require('browser-or-node');
if (isNode) {
console.log('Running in Node.js');
}
Detect if running in a web worker
This feature allows you to check if the code is being executed in a Web Worker environment. The 'isWebWorker' boolean will be true if the code is running in a Web Worker.
const { isWebWorker } = require('browser-or-node');
if (isWebWorker) {
console.log('Running in a Web Worker');
}
The 'is-browser' package provides a simple way to check if the code is running in a browser environment. It is similar to 'browser-or-node' but does not offer the same level of granularity, such as detecting Node.js or Web Workers.
The 'detect-node' package is a lightweight utility to check if the code is running in a Node.js environment. Unlike 'browser-or-node', it does not provide functionality to detect browser or Web Worker environments.
The 'is-node' package is another utility to determine if the code is running in a Node.js environment. It is similar to 'detect-node' and does not offer the ability to detect browser or Web Worker environments.
Check whether the code is running in the browser or node.js runtime.
$ npm install --save browser-or-node
ES6 style import
import { isBrowser, isNode, isWebWorker, isJsDom, isDeno } from "browser-or-node";
if (isBrowser) {
// do browser only stuff
}
if (isNode) {
// do node.js only stuff
}
if (isWebWorker) {
// do web worker only stuff
}
if (isJsDom) {
// do jsdom only stuff
}
if (isDeno) {
// do deno only stuff
}
ES5 style import
var jsEnv = require("browser-or-node");
if (jsEnv.isBrowser) {
// do browser only stuff
}
if (jsEnv.isNode) {
// do node.js only stuff
}
if (jsEnv.isWebWorker) {
// do web worker only stuff
}
if (jsEnv.isJsDom) {
// do jsdom only stuff
}
if (jsEnv.isDeno) {
// do deno only stuff
}
MIT © Dineshkumar Pandiyan
FAQs
Check in which environment the code is running - browser/node.js/webworker/jsdom/deno
We found that browser-or-node demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.