Demo application showing how to share code between NextJS TypeScript projects within a monorepo. Full explanation of how it's built here.
- pnpm workspaces to create a 'monorepo' containing two NextJS applications and a shared package
next-transpile-modules to allow importing of code from one application into another. Simplified as of Next.js version 13, where there is a built-intranspilePackagesoption which can be used instead of the 3rd partynext-transpile-modulespackage.
- Install dependencies with
pnpm i - Start both applications with
pnpm dev:all - View the applications running at
localhost:3001andlocalhost:3002
The code sharing is demonstrated for both frontend and backend (API) code:
- The
app-1homepage defined inapp-1/pages/index.jsuses theopenApp2component fromapp-2; this can be seen working atlocalhost:3001 - The
app-1'hello' API route inapp-1/pages/api/hello.txuses thesayHellofunction fromapp-2; this can be seen working atlocalhost:3001/api/hello