Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
detect-node
Advanced tools
The detect-node npm package is a simple utility that allows developers to determine if their JavaScript code is running in a Node.js environment as opposed to a browser environment. This can be useful for writing isomorphic code that behaves differently depending on where it is executed.
Node.js environment detection
This feature allows the developer to check if the code is running in Node.js. The package exports a boolean value that is true if the environment is Node.js and false otherwise.
const isNode = require('detect-node');
if (isNode) {
console.log('Running in Node.js');
} else {
console.log('Running in the browser');
}
is-node is a package similar to detect-node that provides a simple check to see if the code is running in Node.js. It compares to detect-node by offering the same basic functionality but may have different implementation details or additional features.
is-node-process is another package that serves the same purpose as detect-node. It checks if the current process is a Node.js process. The difference may lie in the specific method of detection and any additional checks or features it provides.
npm install --save detect-node
var isNode = require('detect-node');
if (isNode) {
console.log("Running under Node.JS");
} else {
alert("Hello from browser (or whatever not-a-node env)");
}
The check is performed as:
module.exports = false;
// Only Node.JS has a process variable that is of [[Class]] process
try {
module.exports = Object.prototype.toString.call(global.process) === '[object process]'
} catch(e) {}
Thanks to Ingvar Stepanyan for the initial idea. This check is both the most reliable I could find and it does not use process
env directly, which would cause browserify to include it into the build.
FAQs
Detect Node.JS (as opposite to browser environment) (reliable)
We found that detect-node demonstrated a not healthy version release cadence and project activity because the last version was released 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.