Monorepo TypeScript Demo
A hello world type monorepo demo for TypeScript and Yarn Workspaces.
Before Yarn Workspaces
Withouth workspaces, you have to build and link each project separately. For instance:
$ npm install -g typescript
$ cd shared
$ tsc
This builds the shared
package. But when we try to do the same with sayhi
, we get an error since the local dependency is not found:
$ cd ..
$ cd sayhi
$ tsc
src/sayhi.ts:1:20 - error TS2307: Cannot find module 'shared' or its corresponding type declarations.
1 import { hi } from 'shared';
~~~~~~~~
Found 1 error.
Yarn workspaces help us link projects while keeping each in its own separate folder.
Configure Yarn Workspaces and TypeScript
To configure workspaces, first install the latest Yarn version:
$ yarn set version berry
This creates .yarn
and .yarnrc.yml
Initialize workspaces, this creates the packages
folder…