name: monorepo-predev-pattern
description: "Description: Use when fixing "module not found" errors for @flightdeck/shared during development, or when setting up dev scripts in npm workspace monorepos."
Monorepo predev Script Pattern for Shared Packages
Description: Use when fixing "module not found" errors for @flightdeck/shared during development, or when setting up dev scripts in npm workspace monorepos.
The Problem
In this monorepo:
packages/sharedexports fromdist/(compiled TypeScript)packages/serverdev script usestsx watchwhich transpiles on-the-fly- But
tsxresolves@flightdeck/sharedvia the npm workspace symlink →packages/shared/dist/ - In a fresh environment,
dist/doesn't exist →ERR_MODULE_NOT_FOUND
The Solution
Add predev scripts that build shared before starting the dev server:
Root package.json:
{
"scripts": {
"predev": "npm run build:shared",
"build:shared": "cd packages/shared && rm -f tsconfig.tsbuildinfo && npm run build",
"dev": "node scripts/dev.mjs",
"predev:server": "npm run build:shared"
}
}
packages/server/package.json:
{
"scripts": {
"predev": "cd ../shared && rm -f tsconfig.tsbuildinfo && npm run build",
"dev": "tsx watch src/index.ts"
}
}
Key Details
- npm automatically runs
predevbeforedevandpredev:serverbeforedev:server - Always
rm -f tsconfig.tsbuildinfobefore building (see tsbuildinfo-cache-gotcha skill) - From
packages/server/, usecd ../shared && npm run build(not--workspace— that doesn't work from child dirs) --workspace=packages/sharedonly works from the monorepo root