Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
browser-or-node
Advanced tools
Check in which environment the code is running - browser/node.js/webworker/jsdom/deno
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 in which environment the code is running - browser/node.js/webworker/jsdom/deno.
[!NOTE] To help release v3.0.0, please try v3.0.0-pre.0 and let us know if you run into issues.
$ npm install --save browser-or-node
Import the checks and use it in your code. Works with both ESM and CJS imports.
import * as jsEnv from "browser-or-node";
// import { isBrowser, isNode, isWebWorker, isJsDom, isDeno, isBun } from "browser-or-node";
// const 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
}
if (jsEnv.isBun) {
// do bun only stuff
}
MIT © Dinesh Pandiyan
FAQs
Check in which environment the code is running - browser/node.js/webworker/jsdom/deno
The npm package browser-or-node receives a total of 624,860 weekly downloads. As such, browser-or-node popularity was classified as popular.
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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.