next.js: `next build` fails with webpack error

What version of Next.js are you using?

10.2.1

What version of Node.js are you using?

14.16.0

What browser are you using?

n/a

What operating system are you using?

MacOS, Linux

How are you deploying your application?

AWS

Describe the Bug

When attempting to do a build with next build I get the following error. With next 10.2.0, the error does not appear.

info  - Checking validity of types  
info  - Using external babel configuration from /var/www/sites/customer-ui/.babelrc.js
info  - Creating an optimized production build  
Failed to compile.

chunk webpack [entry]
Cannot read property 'asString' of undefined


> Build error occurred
Error: > Build failed because of webpack errors
    at /var/www/sites/customer-ui/node_modules/next/dist/build/index.js:17:924
    at async Span.traceAsyncFn (/var/www/sites/customer-ui/node_modules/next/dist/telemetry/trace/trace.js:6:584)

Oddly enough, this doesn’t happen when running the build on MacOS…it only occurs when running in our node:14.15.0-alpine docker container.

Expected Behavior

A clean build.

To Reproduce

next build

I’m unsure what to provide to reproduce the error. The message doesn’t give a good indication of where the problem exists or what I might do to provide a minimal project that exercises the issue.

About this issue

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

Most upvoted comments

This problem can be solved by adding future: { webpack5: true } to next.config.js and enabling webpack5. Probably a compatibility issue between webpack4 and webpack5.

@timneutkens Hi check this repo https://github.com/SinimaWath/next-dynamic-css

That bug appears only with dynamic Components in project.

Edit: And it seems because of this PR https://github.com/vercel/next.js/pull/24573/files @sokra.

Without CSS in dynamic chunks, build works fine

@timneutkens Thanks, will do in a few minutes! 👍

I still got the issue on v10.2.3:

info  - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
info  - Skipping validation of types  
info  - Creating an optimized production build  
Failed to compile.

webpack/runtime/compat
The "path" argument must be of type string. Received undefined


> Build error occurred
Error: > Build failed because of webpack errors

The error goes away after i comment out some imports from a worker. That being said, the with-web-worker example works with an import from the worker, so it’s something to do with the imported files in our project. Everything is working on 10.1.3 though.

for me it was f*** node version, was using latest 17.0.1 fixed with nvm to downgrade node to 16.13.0(lts)

Can you open a new issue for that case, as it’s different than the one fixed here 👍

@timneutkens Hmm, @nemanja-tosic is already on v10.2.3 and there are no (related) changes in canary. Btw. I have the same issue - see https://github.com/vercel/next.js/issues/25276#issuecomment-845799268.

I’m using webpack 4 and started receiving this error today in my monorepo after updating next.

Fixed it for now by using "next": "10.2.0" instead of "next": "^10.2.0" (notice the absence of ^) in my dependencies and peerDependencies where appropriate.