next.js: Stuck on `Creating an optimized production build` forever when development server is running

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System: Platform: win32 Arch: x64 Version: Windows 10 Pro Binaries: Node: 16.13.0 npm: N/A Yarn: N/A pnpm: 6.24.0-1 Relevant packages: next: 12.1.4 react: 17.0.2 react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

I had my development server running in the background without me remembering and I wanted to build my app using next build, that’s what I’ve got:

$ yarn next build
yarn run v1.22.17
$ C:\development\activity-builder\node_modules\.bin\next build
info  - Loaded env from C:\development\activity-builder\.env
info  - Checking validity of types  
warn  - The Next.js plugin was not detected in your ESLint configuration. See https://nextjs.org/docs/basic-features/eslint#migrating-existing-config

./src/components/Activities/ActivityQuestions/DynamicQuestionTypes/MultipleChoice.tsx  
68:40  Warning: Forbidden non-null assertion.  @typescript-eslint/no-non-null-assertion
69:22  Warning: Forbidden non-null assertion.  @typescript-eslint/no-non-null-assertion

./src/components/Activities/ActivityQuestions/Question.tsx
102:13  Warning: Unexpected console statement.  no-console

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/basic-features/eslint#disabling-rules
info  - Creating an optimized production build  

And I was stuck for 30 minutes on info - Creating an optimized production build without any indication of anything being wrong, I noticed that the development server was running, after I’ve shut it down and ran next build again It compiled successfully.

Expected Behavior

  • Run next build to compile the app for production even when I’m running the development server.

    OR

  • Notify me that the development server is running and needs to be shut down before running next build, that’s in case running next build when next is running isn’t possible.

Link to reproduction

https://github.com/YassinEldeeb/Next.js-starter

To Reproduce

  1. Run next for the development server.
  2. Then run next build while the development server is running.

Note: It’s not specific to the repository linked above.

About this issue

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

Most upvoted comments

Make sure you don’t run development and build the at the same time. I got my build running forever till I closed my dev server and it completes my build process.

Stuck on Creating an optimized production build forever when development server is running.

Same issue but I wasn’t aware that this was happening due the open dev webserver.

So the solution for me is on your title: close the development server, then run next build and next export (If you intend to deploy statically).

I had this issue when there was code that depended on npm packages that weren’t installed. I added this to my next.config.js to see which packages it was:

process.on('unhandledRejection', error => {
  console.log('unhandledRejection', error);
});

When I run npm run build inside a docker container. It never builds and is for me stuck on “Creating an optimized production build …” as well image

I followed Vercel’s guide on dockerisation and their freshly cloned example also hangs forever. If you check the picture above, mine is still hanging after running for 6200+ seconds (1h+).

The problem is very likely not:

  • Version, everything is up to date as of 06.10.2023
  • RAM, docker has 20 GB of RAM it can use

What I noticed about the docker build command (both tested for fresh Vercel example as well as my own dockerised project(nextjs project with app dir)), is that on macOS Sonoma, it hangs forever. HOWEVER, if I pull the same exact files and build the project on my Linux PopOs machine, it builds just fine.

So for me, it could be a configuration issue or some Docker-Sonoma issue.

Does anyone else see themselves in this issue as well?

You can’t have both dev and build at the same time.

I solved it very quickly when I realized that there were several nodejs processes open when there should only be 1, and killing all the nodejs processes that were running in the background solved it for me.

It can also happen that a build started to fail and got stuck in the background so you should kill all the nodejs you see running in the task manager. I am using Nextjs 13

Facing the same issue

If anyone else is having this issue I’ll just put it out there what was happening to me.

Build was stuck on this state but would eventually finish after 10 minutes. This is a long time, specially on a Mac M1. I found out that while refactoring a few things I forgot to update the content on the tailwind config file. It was reading every .js/.ts file inside my huge /node_modules folder, on every single build.

Once I fixed that the build times went back to normal (30 seconds or less).

When I run npm run build inside a docker container. It never builds and is for me stuck on “Creating an optimized production build …” as well image I followed Vercel’s guide on dockerisation and their freshly cloned example also hangs forever. If you check the picture above, mine is still hanging after running for 6200+ seconds (1h+). The problem is very likely not:

  • Version, everything is up to date as of 06.10.2023
  • RAM, docker has 20 GB of RAM it can use

What I noticed about the docker build command (both tested for fresh Vercel example as well as my own dockerised project(nextjs project with app dir)), is that on macOS Sonoma, it hangs forever. HOWEVER, if I pull the same exact files and build the project on my Linux PopOs machine, it builds just fine. So for me, it could be a configuration issue or some Docker-Sonoma issue. Does anyone else see themselves in this issue as well?

the same. have you solved this problem? i almost tried everything i could do. delete the cache, update dependency to latest version, but it is still stucking on the step. And i tried to build this project in local, it completed very soon. But stuck on the step when i build it in docker.

i fixed that by using OrbStack to use ‘docker build’ command. The problem seems to be caused by Docker Desktop.

This!!!

Docker Desktop is the problem. For anyone trying to run next build in a container, via Docker Desktop on Mac M1/M2.

My builds would often fail, and sporadically work. When it worked, it could take 30 minutes to an hours.

I switched to OrbStack and the build worked flawlessly, in about 2 minutes.

Moving to 13.0.5 worked. Makes me think there is some kind of bug that keeps creeping up in the builder since I see it’s been showing up in different versions going back to 12.x.x

Same Issue. In development it worked, but when I try to create the Docker image it stuck

I solved with the next steps:

  1. Upgrading Next to v12.2.5.
  2. Removing local folders: .next, node_modules
  3. Installing dependencies again: yarn install
  4. Adding this to next-config.js: output: ‘standalone’
  5. Adding the next paths to the .dockerignore: Dockerfile .dockerignore node_modules npm-debug.log README.md .next docker .git
  6. Creating the image again: docker build …

When I run npm run build inside a docker container. It never builds and is for me stuck on “Creating an optimized production build …” as well image

I followed Vercel’s guide on dockerisation and their freshly cloned example also hangs forever. If you check the picture above, mine is still hanging after running for 6200+ seconds (1h+).

The problem is very likely not:

  • Version, everything is up to date as of 06.10.2023
  • RAM, docker has 20 GB of RAM it can use

What I noticed about the docker build command (both tested for fresh Vercel example as well as my own dockerised project(nextjs project with app dir)), is that on macOS Sonoma, it hangs forever. HOWEVER, if I pull the same exact files and build the project on my Linux PopOs machine, it builds just fine.

So for me, it could be a configuration issue or some Docker-Sonoma issue.

Does anyone else see themselves in this issue as well?

In my case, I suspected that it was some virtualization issue, and I was right. Once I disabled Rosetta for x86/amd64 emulation in my Docker desktop config I was able to build my image successfully!

Screenshot 2023-12-01 at 12 18 20

When I run npm run build inside a docker container. It never builds and is for me stuck on “Creating an optimized production build …” as well image I followed Vercel’s guide on dockerisation and their freshly cloned example also hangs forever. If you check the picture above, mine is still hanging after running for 6200+ seconds (1h+). The problem is very likely not:

  • Version, everything is up to date as of 06.10.2023
  • RAM, docker has 20 GB of RAM it can use

What I noticed about the docker build command (both tested for fresh Vercel example as well as my own dockerised project(nextjs project with app dir)), is that on macOS Sonoma, it hangs forever. HOWEVER, if I pull the same exact files and build the project on my Linux PopOs machine, it builds just fine. So for me, it could be a configuration issue or some Docker-Sonoma issue. Does anyone else see themselves in this issue as well?

the same. have you solved this problem? i almost tried everything i could do. delete the cache, update dependency to latest version, but it is still stucking on the step.

And i tried to build this project in local, it completed very soon. But stuck on the step when i build it in docker.

i fixed that by using OrbStack to use ‘docker build’ command. The problem seems to be caused by Docker Desktop.

Another reason for this:

In my case I was running the NextJS 13 project with NodeJS 18.13.0 switched through nvm (my default NodeJS version is v14.13.1), and I’ve forgot that I’ve re-opened the terminal.

When I remembered and switched back to 18.13.0, everything back to normal.

🤦

Same issue happened to me today. And indeed the culprit was dev server running in a vscode terminal that I had forgotten to close.

Which is fine, if you were given some information about this being the case instead of waiting for infinity.

Mine was solved when downgrading from next 13 to next 12.2

I had the same issue and I found out the cause by adding this to my next.config.js - suggested by @andersekdahl

process.on('unhandledRejection', error => {
	console.log('unhandledRejection', error);
});

And I found out that the issue was the next build getting stuck when it couldn’t download the fonts from Google. I turned off the VPN, and it worked.

FWIW: I got finally a clean build by adjusting the versions in package.json to most recent ones, rm -rf .next node_modules; npm cache clean --force; npm update.

Even so npm cache clean emits bloat about self-healing and bla, --force was required to be able to fetch latest versions/resolve dependencies as needed.

Dependencies used:

	"dependencies": {
		"@next-auth/prisma-adapter": "^1.0.6",
		"@prisma/client": "^4.13.0",
		"next": "13.3.4",
		"nodemailer": "^6.9.1",
		"react": "18.2.0",
		"react-dom": "18.2.0"
	},
	"devDependencies": {
		"@types/node": "18.16.3",
		"@types/nodemailer": "^6.4.7",
		"@styled-icons/material": "^10.47.0",
		"@types/react": "18.2.0",
		"@types/react-dom": "18.2.1",
		"eslint": "8.39.0",
		"eslint-config-next": "13.3.4",
		"prisma": "^4.13.0",
		"typescript": "5.0.4"
	}

Mine was solved after closing the dev server.

next 13 with react-router-dom Build log https://vercel.com/wener/apis/GFynQQEqTYxLxPS2GJf6zt3eQTAM Project code https://github.com/wenerme/wode/tree/develop/apps/apis

Same repo, but another project do not have this problem https://github.com/wenerme/wode/tree/develop/apps/demo Build log https://vercel.com/wener/wode/HvcVKX24v6KoA7T3D6mjs6pRTmrv

downgrade to react-router-dom@6.5.0 works

I had the same issue. However, after updating Next.js and react-router-dom to their latest minor versions, the issue went away.

I am on now on: node 18.12.0 Next.js 13.1.4 react-router-dom 6.7.0

and next:build + next:export starts working again.

I have the same issue using the latest version in my bitbucket pipeline however if I rollback to 13.0.7 it builds ok. The pipeline just gets stuck and doesn’t error. I’ve not tried anything in between as yet to find the exact version where the issue starts. Also not seeing this when building locally

next 13 with react-router-dom Build log https://vercel.com/wener/apis/GFynQQEqTYxLxPS2GJf6zt3eQTAM Project code https://github.com/wenerme/wode/tree/develop/apps/apis

Same repo, but another project do not have this problem https://github.com/wenerme/wode/tree/develop/apps/demo Build log https://vercel.com/wener/wode/HvcVKX24v6KoA7T3D6mjs6pRTmrv


downgrade to react-router-dom@6.5.0 works

I faced the same error using next v13, updating to 13.0.5 the error is gone.