Search code examples
node.jsyarnpkg

How can I execute a bin with yarn?


I have the following package.json and I'd like to run the bins "build" and "run":

{
  "name":    "simple-site",
  "version": "0.0.5",
  "license": "MIT",
  "bin": {
    "build": "./bin/build.js",
    "dev":   "./bin/dev.js"
  }
}

I've tried:

yarn run build

and I get

error Command "build" not found.

I've also tried:

yarn build

but the same thing happens:

error Command "build" not found.

It's propably not the right way to run bins. But then again, what is the right way to run bins with yarn?


Solution

  • Your package isn't installed.

    When Yarn (and NPM) installs your package, it adds the commands under node_modules/.bin/, e.g. node_modules/.bin/build. Running yarn build would (if it doesn't find a matching script in the current package) look for a build in this .bin, then traverse upwards through the filesystem, looking for other node_modules/.bin/build's.

    If your build script is only meant to be run while developing that specific package, add it as a script (see example here). It would more or less look like this:

    {
      "name":    "simple-site",
      "version": "0.0.5",
      "license": "MIT",
      "scripts": {
        "build": "node ./bin/build.js",
        "dev":   "node ./bin/dev.js"
      }
    }