NextJs Monorepo deploy

Hello,

I’m confused about how to deploy from a monorepo. I am running a NextJs App the package name is web.

The project structure

apps/
> web/
> server/

netlify.toml
package.json

Inside the netlify.toml

[build]
  base = "app/web"

  publish = "apps/web/.next"

  # Default build command.
  command = "npm run build"

These are the logs

1:41:32 PM: Build ready to start
1:41:33 PM: build-image version: d05976bd3309b73a86eaa327bf80e105ff13f5e4 (focal)
1:41:33 PM: build-image tag: v4.13.0
1:41:33 PM: buildbot version: f9b3ce49f3adc0f38a145dbc82e4c63f31681c42
1:41:33 PM: Fetching cached dependencies
1:41:33 PM: Starting to download cache of 137.4MB
1:41:34 PM: Finished downloading cache in 1.086014761s
1:41:34 PM: Starting to extract cache
1:41:36 PM: Finished extracting cache in 1.152625807s
1:41:36 PM: Finished fetching cache in 2.279525627s
1:41:36 PM: Starting to prepare the repo for build
1:41:36 PM: Preparing Git Reference refs/heads/main
1:41:36 PM: Parsing package.json dependencies
1:41:37 PM: Creating deploy upload records
1:41:37 PM: Failing build: Failed to parse configuration
1:41:37 PM: Failed during stage 'Reading and parsing configuration files': 
When resolving config file /opt/build/repo/netlify.toml:
Base directory does not exist: /opt/build/repo/app/web

: exit status 1
1:41:37 PM: Finished processing build request in 4.205957373s

If I remove the base directory it will build successfully but not publish (page not found). I’ve tried every kind of variation but nothing is working.

Hi @Jamic16,
It would be difficult to tell what is causing the issue since I cannot tell the contents of the folders web and server and the related settings in your package.json file.
if possible can you share a repository of your site for me to help with the debugging.

Thanks.

From the look of it @Jamic16, this is a matter of a typo.

First you have

but then you have

Is the parent directory app or apps?

Sorry it is apps/web

I am using Turbo. here is the package.json

{
  "name": "shatzen",
  "version": "0.0.0",
  "private": true,
  "workspaces": [
    "apps/*",
    "packages/*"
  ],
  "scripts": {
    "build": "turbo run build",
    "dev": "turbo run dev --parallel",
    "lint": "turbo run lint",
    "format": "prettier --write \"**/*.{ts,tsx,md}\"",
    "prepare": "husky install"
  },
  "devDependencies": {
    "@commitlint/cli": "^17.1.2",
    "@commitlint/config-conventional": "^17.1.0",
    "eslint-config-custom": "*",
    "husky": "^8.0.1",
    "lint-staged": "^13.0.3",
    "prettier": "latest",
    "turbo": "latest"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "packageManager": "npm@8.1.2"
}

Have you changed the base directory to apps/web then? This should fix the above error.

Hi coelmay,

This is the logs when I do that

7:27:17 PM: Build ready to start
7:27:19 PM: build-image version: 8b79b5e0c585f1b3f892b8032389b4b4b188d887 (focal)
7:27:19 PM: build-image tag: v4.14.0
7:27:19 PM: buildbot version: 1e88a9b854b49555d21dc539e2b8bf5fb78a8f57
7:27:19 PM: Fetching cached dependencies
7:27:19 PM: Starting to download cache of 157.1MB
7:27:21 PM: Finished downloading cache in 1.613880295s
7:27:21 PM: Starting to extract cache
7:27:22 PM: Finished extracting cache in 1.389828659s
7:27:22 PM: Finished fetching cache in 3.061697597s
7:27:22 PM: Starting to prepare the repo for build
7:27:22 PM: Preparing Git Reference refs/heads/main
7:27:23 PM: Parsing package.json dependencies
7:27:24 PM: Different build dir detected, going to use the one specified in the Netlify configuration file: 'apps/web' versus '' in the Netlify UI
7:27:24 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'apps/web/apps/web/.next' versus '' in the Netlify UI
7:27:24 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'npm run build' versus '' in the Netlify UI
7:27:24 PM: Starting build script
7:27:24 PM: Installing dependencies
7:27:24 PM: Python version set to 2.7
7:27:24 PM: Started restoring cached node version
7:27:25 PM: Finished restoring cached node version
7:27:25 PM: v16.18.0 is already installed.
7:27:25 PM: Now using node v16.18.0 (npm v8.19.2)
7:27:25 PM: Enabling node corepack
7:27:26 PM: Started restoring cached build plugins
7:27:26 PM: Finished restoring cached build plugins
7:27:26 PM: Attempting ruby version 2.7.2, read from environment
7:27:26 PM: Using ruby version 2.7.2
7:27:26 PM: Using PHP version 8.0
7:27:26 PM: No npm workspaces detected
7:27:26 PM: Started restoring cached node modules
7:27:27 PM: Finished restoring cached node modules
7:27:27 PM: Installing NPM modules using NPM version 8.19.2
7:27:35 PM: > shatzen@0.0.0 prepare
7:27:35 PM: > husky install
7:27:35 PM: sh: 1: husky: not found
7:27:35 PM: npm ERR! code 127
7:27:35 PM: npm ERR! path /opt/build/repo
7:27:35 PM: npm ERR! command failed
7:27:35 PM: npm ERR! command sh -c -- husky install
7:27:35 PM: npm ERR! A complete log of this run can be found in:
7:27:35 PM: npm ERR!     /opt/buildhome/.npm/_logs/2022-10-21T23_27_27_437Z-debug-0.log
7:27:35 PM: Error during NPM install
7:27:35 PM: Build was terminated: Build script returned non-zero exit code: 1
7:27:35 PM: Creating deploy upload records
7:27:35 PM: Failing build: Failed to build site
7:27:35 PM: Failed during stage 'building site': Build script returned non-zero exit code: 1 (https://ntl.fyi/exit-code-1)
7:27:35 PM: Finished processing build request in 15.874320263s

Have you read over the Netlify monorepo documentation?

In the documentation it states

…all necessary dependencies are declared in configuration files within each subdirectory. The root-level package.json is used to define a Yarn or npm workspace.

So, unless you have a package.json in the apps/web directory, none of the dependencies for the project are installed.

Hi yes, I have read it. I think I made some progress but still not working. I removed the build command from the top-level package.json because it runs a turbo repo script. In the netlify.toml I added the next.js plugin. So it seems like it should work so now it builds but these are the logs

8:13:02 PM: Build ready to start
8:13:37 PM: build-image version: 8b79b5e0c585f1b3f892b8032389b4b4b188d887 (focal)
8:13:37 PM: build-image tag: v4.14.0
8:13:37 PM: buildbot version: 1e88a9b854b49555d21dc539e2b8bf5fb78a8f57
8:13:37 PM: Fetching cached dependencies
8:13:37 PM: Starting to download cache of 98.8MB
8:13:38 PM: Finished downloading cache in 773.074714ms
8:13:38 PM: Starting to extract cache
8:13:39 PM: Finished extracting cache in 616.076648ms
8:13:39 PM: Finished fetching cache in 1.441964274s
8:13:39 PM: Starting to prepare the repo for build
8:13:39 PM: Preparing Git Reference refs/heads/main
8:13:39 PM: Parsing package.json dependencies
8:13:40 PM: Different build dir detected, going to use the one specified in the Netlify configuration file: 'apps/web' versus '' in the Netlify UI
8:13:40 PM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'apps/web/apps/web/.next' versus '' in the Netlify UI
8:13:40 PM: Different build command detected, going to use the one specified in the Netlify configuration file: 'npm run build' versus '' in the Netlify UI
8:13:40 PM: Starting build script
8:13:40 PM: Installing dependencies
8:13:40 PM: Python version set to 2.7
8:13:41 PM: Started restoring cached node version
8:13:41 PM: Finished restoring cached node version
8:13:41 PM: v16.18.0 is already installed.
8:13:42 PM: Now using node v16.18.0 (npm v8.19.2)
8:13:42 PM: Enabling node corepack
8:13:42 PM: Started restoring cached build plugins
8:13:42 PM: Finished restoring cached build plugins
8:13:42 PM: Attempting ruby version 2.7.2, read from environment
8:13:42 PM: Using ruby version 2.7.2
8:13:42 PM: Using PHP version 8.0
8:13:43 PM: No npm workspaces detected
8:13:43 PM: Started restoring cached node modules
8:13:43 PM: Finished restoring cached node modules
8:13:43 PM: Installing NPM modules using NPM version 8.19.2
8:13:50 PM: added 510 packages, and audited 516 packages in 7s
8:13:50 PM: 113 packages are looking for funding
8:13:50 PM:   run `npm fund` for details
8:13:50 PM: found 0 vulnerabilities
8:13:50 PM: NPM modules installed
8:13:50 PM: Started restoring cached go cache
8:13:50 PM: Finished restoring cached go cache
8:13:50 PM: Installing Go version 1.17 (requested 1.17)
8:13:55 PM: unset GOOS;
8:13:55 PM: unset GOARCH;
8:13:55 PM: export GOROOT='/opt/buildhome/.gimme/versions/go1.17.linux.amd64';
8:13:55 PM: export PATH="/opt/buildhome/.gimme/versions/go1.17.linux.amd64/bin:${PATH}";
8:13:55 PM: go version >&2;
8:13:55 PM: export GIMME_ENV="/opt/buildhome/.gimme/env/go1.17.linux.amd64.env"
8:13:55 PM: go version go1.17 linux/amd64
8:13:55 PM: Installing missing commands
8:13:55 PM: Verify run directory
8:13:56 PM: ​
8:13:56 PM: ────────────────────────────────────────────────────────────────
8:13:56 PM:   Netlify Build                                                 
8:13:56 PM: ────────────────────────────────────────────────────────────────
8:13:56 PM: ​
8:13:56 PM: ❯ Version
8:13:56 PM:   @netlify/build 28.1.1
8:13:56 PM: ​
8:13:56 PM: ❯ Flags
8:13:56 PM:   baseRelDir: true
8:13:56 PM:   buildId: 6353358e707bb600095fb33d
8:13:56 PM:   deployId: 6353358e707bb600095fb33f
8:13:56 PM: ​
8:13:56 PM: ❯ Current directory
8:13:56 PM:   /opt/build/repo/apps/web
8:13:56 PM: ​
8:13:56 PM: ❯ Config file
8:13:56 PM:   /opt/build/repo/netlify.toml
8:13:56 PM: ​
8:13:56 PM: ❯ Context
8:13:56 PM:   production
8:13:56 PM: ​
8:13:56 PM: ❯ Using Next.js Runtime - v4.27.3
8:13:57 PM: ​
8:13:57 PM: ────────────────────────────────────────────────────────────────
8:13:57 PM:   1. @netlify/plugin-nextjs (onPreBuild event)                  
8:13:57 PM: ────────────────────────────────────────────────────────────────
8:13:57 PM: ​
8:13:57 PM: Next.js cache restored.
8:13:57 PM: Netlify configuration property "build.environment.NEXT_PRIVATE_TARGET" value changed.
8:13:57 PM: ​
8:13:57 PM: (@netlify/plugin-nextjs onPreBuild completed in 77ms)
8:13:57 PM: ​
8:13:57 PM: ────────────────────────────────────────────────────────────────
8:13:57 PM:   2. build.command from netlify.toml                            
8:13:57 PM: ────────────────────────────────────────────────────────────────
8:13:57 PM: ​
8:13:57 PM: $ npm run build
8:13:57 PM: > web@0.1.0 build
8:13:57 PM: > npm install && next build
8:13:59 PM: up to date, audited 516 packages in 1s
8:13:59 PM: 113 packages are looking for funding
8:13:59 PM:   run `npm fund` for details
8:13:59 PM: found 0 vulnerabilities
8:13:59 PM: warn  - No build cache found. Please configure build caching for faster rebuilds. Read more: https://nextjs.org/docs/messages/no-cache
8:13:59 PM: info  - Linting and checking validity of types...
8:14:02 PM: info  - Creating an optimized production build...
8:14:08 PM: info  - Compiled successfully
8:14:08 PM: info  - Collecting page data...
8:14:09 PM: info  - Generating static pages (0/5)
8:14:10 PM: info  - Generating static pages (1/5)
8:14:10 PM: info  - Generating static pages (2/5)
8:14:10 PM: info  - Generating static pages (3/5)
8:14:10 PM: info  - Generating static pages (5/5)
8:14:10 PM: info  - Finalizing page optimization...
8:14:10 PM: Route (pages)                              Size     First Load JS
8:14:10 PM: ┌ ○ / (471 ms)                             2.47 kB         140 kB
8:14:10 PM: ├   /_app                                  0 B             106 kB
8:14:10 PM: ├ ○ /404                                   193 B           106 kB
8:14:10 PM: ├ λ /api/hello                             0 B             106 kB
8:14:10 PM: ├ ○ /login/[uuid] (452 ms)                 2.19 kB         139 kB
8:14:10 PM: └ ○ /room/[room] (453 ms)                  2.72 kB         140 kB
8:14:10 PM: + First Load JS shared by all              108 kB
8:14:10 PM:   ├ chunks/framework-1470d73b4587dc44.js   44.9 kB
8:14:10 PM:   ├ chunks/main-7eb41630595c1fa6.js        31 kB
8:14:10 PM:   ├ chunks/pages/_app-db8f74d2c4f4e7a4.js  28.9 kB
8:14:10 PM:   ├ chunks/webpack-9b312e20a4e32339.js     836 B
8:14:10 PM:   └ css/97f49350277676a7.css               2.74 kB
8:14:10 PM: λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
8:14:10 PM: ○  (Static)  automatically rendered as static HTML (uses no initial props)
8:14:10 PM: ​
8:14:10 PM: (build.command completed in 13.2s)
8:14:10 PM: ​
8:14:10 PM: ────────────────────────────────────────────────────────────────
8:14:10 PM:   3. @netlify/plugin-nextjs (onBuild event)                     
8:14:10 PM: ────────────────────────────────────────────────────────────────
8:14:10 PM: ​
8:14:11 PM: ​
8:14:11 PM: ────────────────────────────────────────────────────────────────
8:14:11 PM:   Plugin "@netlify/plugin-nextjs" failed                        
8:14:11 PM: ────────────────────────────────────────────────────────────────
8:14:11 PM: ​
8:14:11 PM:   Error message
8:14:11 PM:   Error: The directory "/opt/build/repo/apps/web/apps/web/.next" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
8:14:11 PM:   In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.
8:14:11 PM:   If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
8:14:11 PM: ​
8:14:11 PM:   Plugin details
8:14:11 PM:   Package:        @netlify/plugin-nextjs
8:14:11 PM:   Version:        4.27.3
8:14:11 PM:   Repository:     git+https://github.com/netlify/next-runtime.git
8:14:11 PM:   npm link:       https://www.npmjs.com/package/@netlify/plugin-nextjs
8:14:11 PM:   Report issues:  https://github.com/netlify/next-runtime/issues
8:14:11 PM: ​
8:14:11 PM:   Error location
8:14:11 PM:   In "onBuild" event in "@netlify/plugin-nextjs" from netlify.toml and package.json
8:14:11 PM: ​
8:14:11 PM:   Resolved config
8:14:11 PM:   build:
8:14:11 PM:     base: /opt/build/repo/apps/web
8:14:11 PM:     command: npm run build
8:14:11 PM:     commandOrigin: config
8:14:11 PM:     environment:
8:14:11 PM:       - NEXT_PUBLIC_SERVER
8:14:11 PM:       - NEXT_PRIVATE_TARGET
8:14:11 PM:     publish: /opt/build/repo/apps/web/apps/web/.next
8:14:11 PM:     publishOrigin: config
8:14:11 PM:   plugins:
8:14:11 PM:     - inputs: {}
8:14:11 PM:       origin: config
8:14:11 PM:       package: '@netlify/plugin-nextjs'
8:14:11 PM: Caching artifacts
8:14:11 PM: Started saving node modules
8:14:11 PM: Finished saving node modules
8:14:11 PM: Started saving build plugins
8:14:11 PM: Finished saving build plugins
8:14:11 PM: Started saving pip cache
8:14:11 PM: Finished saving pip cache
8:14:11 PM: Started saving emacs cask dependencies
8:14:11 PM: Finished saving emacs cask dependencies
8:14:11 PM: Started saving maven dependencies
8:14:11 PM: Finished saving maven dependencies
8:14:11 PM: Started saving boot dependencies
8:14:11 PM: Finished saving boot dependencies
8:14:11 PM: Started saving rust rustup cache
8:14:11 PM: Finished saving rust rustup cache
8:14:11 PM: Started saving go dependencies
8:14:11 PM: Finished saving go dependencies
8:14:11 PM: Build failed due to a user error: Build script returned non-zero exit code: 2
8:14:11 PM: Creating deploy upload records
8:14:11 PM: Failing build: Failed to build site
8:14:11 PM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
8:14:11 PM: Finished processing build request in 34.355879348s

This error happens with or without the plugin actually. Is there any way I can see the file structure after the build? I want to see what is going on. If I build locally it definitely builds to a .next folder

This is without the plugin

8:21:28 PM:   Error message
8:21:28 PM:   Deploy did not succeed: Deploy directory 'apps/web/apps/web/.next' does not exist

Why is it adding the apps/web/app/web/.next???

What are the build settings you are using?

AH yes, I see that now.

Thank you so much for the help.

Please don’t post duplicates: