nx: Command to build Next.js for Vercel errors out

Current Behavior

Command fails

Expected Behavior

I expected it to build my project successfully

Steps to Reproduce

Repo: https://github.com/Xenfo/atomic Try to deploy it to Vercel following the guide in the NX Next.js docs.

Failure Logs

❯ nx build frontend --prod --outputPath=.

> nx run frontend:build:production --outputPath=. 
info  - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
info  - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
info  - Checking validity of types  
info  - Using external babel configuration from /home/xenfo/Desktop/Projects/atomic/apps/frontend/.babelrc
info  - Creating an optimized production build  
info  - Compiled successfully
info  - Collecting page data  
info  - Generating static pages (3/3)
info  - Finalizing page optimization  

Page                                                           Size     First Load JS
┌ ○ /                                                          1.22 kB        68.4 kB
├   └ css/f06ddab96869c260cfa8.css                             20 B
├   /_app                                                      0 B            67.2 kB
└ ○ /404                                                       3.73 kB        70.9 kB
+ First Load JS shared by all                                  67.2 kB
  ├ chunks/f6078781a05fe1bcb0902d23dbbb2662c8d200b3.9f24d5.js  13.6 kB
  ├ chunks/framework.0c3b20.js                                 41.8 kB
  ├ chunks/main.bab312.js                                      6.95 kB
  ├ chunks/pages/_app.4b9835.js                                4.15 kB
  ├ chunks/webpack.50bee0.js                                   751 B
  └ css/48cbfac088811e35fdc5.css                               667 B

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)
●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
   (ISR)     incremental static regeneration (uses revalidate in getStaticProps)


———————————————————————————————————————————————

>  NX   ERROR  Running target "frontend:build" failed

  Failed tasks:
  
  - frontend:build:production


>  NX   CLOUD  See run details at ...

Environment

Node : 14.15.5
OS   : linux x64
yarn : 1.22.5
  
nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 12.3.4
@nrwl/cypress : 12.3.4
@nrwl/devkit : 12.3.4
@nrwl/eslint-plugin-nx : 12.3.4
@nrwl/express : 12.3.4
@nrwl/jest : 12.3.4
@nrwl/linter : 12.3.4
@nrwl/nest : Not Found
@nrwl/next : 12.3.4
@nrwl/node : 12.3.4
@nrwl/react : 12.3.4
@nrwl/schematics : Not Found
@nrwl/tao : 12.3.4
@nrwl/web : 12.3.4
@nrwl/workspace : 12.3.4
@nrwl/storybook : 12.3.4
@nrwl/gatsby : Not Found
typescript : 4.2.4

Workaround from Vercel

Set custom build command to: npx nx build my-app --prod Set output directory to: dist/apps/my-app/.next

Note for this to work you must have NX Cloud off as far as I can tell

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (2 by maintainers)

Most upvoted comments

@hcharley Just want to note that when Vercel tries to build with NX build cache it fails, so if you try to rebuild it probably won’t work.


13:49:13.503 | Cloning github.com/Xenfo/atomic (Branch: dev, Commit: 9ee2551)
-- | --
13:49:13.792 | Cloning completed: 288.897ms
13:49:13.830 | Analyzing source code...
13:49:15.052 | Installing build runtime...
13:49:18.644 | Build runtime installed: 3.592s
13:49:21.635 | Looking up build cache...
13:49:22.003 | Build cache found. Downloading...
13:49:26.619 | Build cache downloaded [60.43 MB]: 4615.908ms
13:49:27.874 | Installing dependencies...
13:49:28.319 | yarn install v1.22.10
13:49:28.445 | [1/4] Resolving packages...
13:49:29.393 | success Already up-to-date.
13:49:29.397 | $ is-ci \|\| husky install
13:49:29.526 | Done in 1.21s.
13:49:29.545 | Detected Next.js version: 10.2.2
13:49:29.786 | Running "yarn nx run frontend:build-css && yarn nx build frontend --prod && rm -rf package.json && mv -v dist/apps/frontend/.next .next && mv -v dist/apps/frontend/package.json package.json && mv -v dist/apps/frontend/next.config.js next.config.js"
13:49:30.144 | yarn run v1.22.10
13:49:30.185 | $ nx run frontend:build-css
13:49:30.961 | > nx run frontend:build-css
13:49:31.471 | $ /vercel/path0/node_modules/.bin/postcss /vercel/path0/apps/frontend/src/styles/main.css -o /vercel/path0/apps/frontend/src/styles/tailwind.css
13:49:32.307 | warn - You have enabled the JIT engine which is currently in preview.
13:49:32.308 | warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
13:49:32.656 | ———————————————————————————————————————————————
13:49:32.656 | >  NX   SUCCESS  Running target "build-css" succeeded
13:49:32.665 | >  NX   CLOUD  See run details at https://nx.app/runs/8xVsN1Ln6w2
13:49:32.682 | Done in 2.55s.
13:49:33.055 | yarn run v1.22.10
13:49:33.101 | $ nx build frontend --prod
13:49:33.583 | > nx run frontend:build:production
13:49:35.448 | info  - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
13:49:35.676 | withNx() plugin: Detected Vercel build environment, applying "experimental-serverless-trace" target
13:49:36.123 | info  - Using webpack 4. Reason: custom webpack configuration in next.config.js https://nextjs.org/docs/messages/webpack5
13:49:36.148 | Attention: Next.js now collects completely anonymous telemetry regarding usage.
13:49:36.148 | This information is used to shape Next.js' roadmap and prioritize features.
13:49:36.148 | You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
13:49:36.148 | https://nextjs.org/telemetry
13:49:36.203 | info  - Checking validity of types...
13:49:39.198 | info  - Creating an optimized production build...
13:49:40.460 | info  - Using external babel configuration from /vercel/path0/apps/frontend/.babelrc
13:49:43.901 | warn - You have enabled the JIT engine which is currently in preview.
13:49:43.901 | warn - Preview features are not covered by semver, may introduce breaking changes, and can change at any time.
13:49:49.886 | info  - Compiled successfully
13:49:49.886 | info  - Collecting page data...
13:49:50.208 | info  - Generating static pages (0/3)
13:49:50.508 | info  - Generating static pages (3/3)
13:49:50.508 | info  - Finalizing page optimization...
13:49:50.524 | Page                                                           Size     First Load JS
13:49:50.524 | ┌ ○ /                                                          1.2 kB         68.6 kB
13:49:50.524 | ├   /_app                                                      0 B            67.4 kB
13:49:50.524 | └ ○ /404                                                       3.73 kB        71.2 kB
13:49:50.524 | + First Load JS shared by all                                  67.4 kB
13:49:50.524 | ├ chunks/f6078781a05fe1bcb0902d23dbbb2662c8d200b3.2702ed.js  13.6 kB
13:49:50.524 | ├ chunks/framework.0c3b20.js                                 41.8 kB
13:49:50.524 | ├ chunks/main.19ac90.js                                      7.2 kB
13:49:50.524 | ├ chunks/pages/_app.256e46.js                                4.16 kB
13:49:50.525 | ├ chunks/webpack.50bee0.js                                   751 B
13:49:50.525 | └ css/ed7e1c278348e35aaa70.css                               1.78 kB
13:49:50.525 | λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
13:49:50.525 | ○  (Static)  automatically rendered as static HTML (uses no initial props)
13:49:50.525 | ●  (SSG)     automatically generated as static HTML + JSON (uses getStaticProps)
13:49:50.525 | (ISR)     incremental static regeneration (uses revalidate in getStaticProps)
13:49:50.525 | Next.js Analytics is enabled for this production build. You'll receive a Real Experience Score computed by all of your visitors.
13:49:51.172 | ———————————————————————————————————————————————
13:49:51.173 | >  NX   SUCCESS  Running target "build" succeeded
13:49:51.179 | >  NX   CLOUD  See run details at https://nx.app/runs/wzs3i3GzYkZ
13:49:51.185 | Done in 18.14s.
13:49:51.199 | ‘dist/apps/frontend/.next’ -> ‘.next/.next’
13:49:51.201 | ‘dist/apps/frontend/package.json’ -> ‘package.json’
13:49:51.202 | ‘dist/apps/frontend/next.config.js’ -> ‘next.config.js’
13:49:51.210 | Error: The file "/vercel/path0/.next/routes-manifest.json" couldn't be found. This is normally caused by a misconfiguration in your project.
13:49:51.210 | Please check the following, and reach out to support if you cannot resolve the problem:
13:49:51.210 | 1. If present, be sure your `build` script in "package.json" calls `next build`.  2. Navigate to your project's settings in the Vercel dashboard, and verify that the "Build Command" is not overridden, or that it calls `next build`.  3. Navigate to your project's settings in the Vercel dashboard, and verify that the "Output Directory" is not overridden. Note that `next export` does **not** require you change this setting, even if you customize the `next export` output directory. Learn More: https://err.sh/vercel/vercel/now-next-routes-manifest

@Xenfo I can confirm that using the cache fails with my method.

Without the cache, my method still works.

It’s still a workaround, so yes @kirjai understanding why --outputPath=. is not working, would be helpful.

I can confirm I’m seeing the same problem with my own freshly created repo.

This fails:

yarn nx build my-app --prod --outputPath=.

This works:

yarn nx build my-app --prod

You can try this locally as well. It is not just happening on Vercel, for me.

I’ve switched to using the solution mentioned last by @Bodom78 which felt cleaner:

https://github.com/nrwl/nx/issues/5660#issuecomment-841639368

hcharley’s method worked for me I’ll be trying DuyBui1993’s method later.

Got hit by this issue as well, this solution at least allowed me to get the build working on Vercel.

After upgrade @nrwl/* from version 12.1.1 to 12.3.4, my vercel website got build failed too.

I got vercel build working by using this workaround