Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including yarn-or-npm with all npm packages installed. Try it out:

var yarnOrNpm = require("yarn-or-npm")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

yarn-or-npm v3.0.1

Use Yarn or npm to execute a command


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:

  1. yarn.lock file is in the nearest package directory - yarn
  2. package-lock.json file is in the nearest package directory - npm
  3. yarn is installed - yarn
  4. Fallback - npm


import yarnOrNpm, { spawn, hasYarn, hasNpm } from 'yarn-or-npm';

// String of `yarn` or `npm` returned

// Boolean values for hasYarn, hasNpm

// Spawn yarn or npm command

// 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)

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"


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free