23

Context

Yarn workspaces provide for a convenient mono-repo-like functionality where packages are automatically linked. I.e. they can require/import each other, and their binaries are linked and accessible from the workspace root.

An example:

workspace/package.json

{
  "name": "200180719-yarn-bin",
  "version": "1.0.0",
  "workspaces": [
    "packages/*"
  ],
  "private": true
}

workspace/packages/a/package.json

{
  "name": "a",
  "version": "1.0.0",
  "main": "src/index.js",
  "bin": {
    "mycli": "src/index.js"
  }
}

workspace/packages/a/src/index.js

#!/usr/bin/env node

console.log('welcome to the cli')

If you then change directory to the main workspace and run yarn install, yarn correctly links the binaries and you can run:

yarn run mycli

From the workspace directory just fine. Great!

Problem

The problem I have is that if your code first has to be compiled, the binary won't be available before yarn install has finished (since you are not supposed to ship compiled code in version control). I use Typescript to compile my cli:

Rename index.js to index.ts and update a/package.json to:

{
  "name": "a",
  "version": "1.0.0",
  "main": "src/index.js",
  "bin": {
    "mycli": "dist/index.js"
  },
  "scripts": {
    "build": "tsc src/index.ts --outDir dist",
    "preinstall": "yarn run build"
  },
  "devDependencies": {
    "typescript": "^2.9.2"
  }
}

Even though dist/index.js is correctly build when you run yarn install on the workspace directory, it fails to create a link to the binary:

Toms-MacBook-Pro-2:200180719-yarn-bin tommedema$ yarn run mycli
yarn run v1.7.0
error Command "mycli" not found.

Question

How can I make yarn workspace binary linking work when my binaries need to be compiled on install?

If I need to use Lerna to make this work, that would be totally fine too (though preferably avoiding a call to lerna bootstrap since that should be redundant with yarn workspaces).

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.