nextjs-sharing-code-monorepo
Demo application showing how to share code between NextJS TypeScript projects within a monorepo. Full explanation of how it's built here.
How it works
- yarn workspaces to create a 'monorepo' containing two NextJS applications
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-intranspilePackages
option which can be used instead of the 3rd partynext-transpile-modules
package.
Getting started
- Install dependencies with
yarn
- Start both applications with
yarn dev:all
- View the applications running at
localhost:3001
andlocalhost:3002
The code sharing is demonstrated for both frontend and backend (API) code:
- The
app-1
homepage defined inapp-1/pages/index.js
uses theopenApp2
component fromapp-2
; this can be seen working atlocalhost:3001
- The
app-1
'hello' API route inapp-1/pages/api/hello.tx
uses thesayHello
function fromapp-2
; this can be seen working atlocalhost:3001/api/hello