Code Monkey home page Code Monkey logo

nextjs-monorepo-example's Introduction

In this project I would like to explain how to convert your Next.js project to monorepository project within 10 minutes.

Getting Started

1. You need to update your project's structure like this:

.
|-- apps
	|-- project-one
	   |-- pages
	   |-- next.config.js
	   |-- tsconfig.json
	|-- project-two
	   |-- pages
	   |-- next.config.js
	   |-- tsconfig.json
|-- libs
	|-- common-component
		|-- common-component.tsx
|-- package.json
|-- tsconfig.json

2. Create in the root new tsconfig.json and then add follow params:

{
	"compilerOptions": {
		// ...
		"baseUrl": ".",
		"paths": {
			"@common/common-component": ["libs/common-component"]
		}
	}
}

3. Add to project nextjs-monorepo-tools package as dependency.

4. Add custom Webpack configuration to next.config.js:

const { patchWebpackConfig } = require("nextjs-monorepo-tools");

const config = {
  webpack: patchWebpackConfig({
    commonDirs: ["libs"], // "libs" is directory for shared modules
  }),
};

module.exports = config;

That should be done for every project in your monorepo.

5. Update package.json

Because how we have two projects we have to update package.json to have dev, build, start commands for each project.

"scripts": {
	"dev:project-one": "next dev apps/project-one",
	"dev:project-two": "next dev apps/project-two",
	"build:project-one": "next build apps/project-one",
	"build:project-two": "next build apps/project-two",
	"start:project-one": "next start apps/project-one",
	"start:project-two": "next start apps/project-two"
}

That's all!

How to add new common module?

Just create new directory in libs and add new section in tsconfig.json.

{
	"compilerOptions": {
		// ...
		"paths": {
			"@common/common-component": ["libs/common-component"],
			"@common/common-component-two": ["libs/common-component-two"],
		}
	}
}

nextjs-monorepo-example's People

Contributors

dkaledin avatar

Stargazers

Shun Kakinoki avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.