![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
yarn-or-npm
Advanced tools
The yarn-or-npm package is a utility that helps developers determine whether to use Yarn or npm for running scripts and installing dependencies. It provides a simple API to detect the package manager being used in a project and execute commands accordingly.
Detecting the package manager
This feature allows you to detect whether Yarn or npm is being used in the current project. The code sample demonstrates how to require the yarn-or-npm package, call it to determine the package manager, and log the result.
const yarnOrNpm = require('yarn-or-npm');
const packageManager = yarnOrNpm();
console.log(`Using ${packageManager}`);
Running commands with the detected package manager
This feature allows you to run commands using the detected package manager. The code sample shows how to use the execSync function from the child_process module to run the install command with the detected package manager.
const yarnOrNpm = require('yarn-or-npm');
const { execSync } = require('child_process');
const packageManager = yarnOrNpm();
execSync(`${packageManager} install`);
The which-pm-runs package is a utility that detects which package manager (npm, Yarn, or pnpm) initiated the current process. Unlike yarn-or-npm, which-pm-runs focuses solely on detection and does not provide functionality for running commands.
The preferred-pm package determines the preferred package manager for a given project by reading the project's configuration files. It supports npm, Yarn, and pnpm. While yarn-or-npm focuses on detecting and running commands, preferred-pm is more about identifying the preferred package manager based on project settings.
Execute scripts with Yarn or npm.
yarn add -D yarn-or-npm
# or
npm i --save-dev yarn-or-npm
The client is determined by a series of ordered checks:
yarn.lock
file is in the nearest package directory - yarnpackage-lock.json
file is in the nearest package directory - npmyarn
is installed - yarnimport yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';
// String of `yarn` or `npm` returned
console.log(yarnOrNpm());
// Boolean values for hasYarn, hasNpm
console.log(hasYarn());
// Spawn yarn or npm command
spawn(['init']);
// Spawn sync option
spawn.sync(['init'], { stdio: 'inherit' });
Under the covers, there are cached lookup values being used for efficiency. These can be manually cleared:
import yarnOrNpm from 'yarn-or-npm';
import { spawnSync } from 'child_process';
console.log(yarnOrNpm.hasYarn()); // false
spawnSync('npm', ['i', '-g', 'yarn'], { stdio: 'inherit' });
console.log(yarnOrNpm.hasYarn()); // false (cached)
yarnOrNpm.clearCache();
console.log(yarnOrNpm.hasYarn()); // true
yarn-or-npm <command>
# Can also use `yon` shorthand
yon <command>
Modules with bin files can be called directly in package.json
scripts:
{
"devDependencies": {
...
"yarn-or-npm": "^1.0.0"
},
"scripts": {
"compile": "babel src --out-dir dist",
"lint": "eslint .",
"prepublish": "yarn-or-npm run lint && yarn-or-npm run compile"
}
}
FAQs
Use Yarn or npm to execute a command
We found that yarn-or-npm 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
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.