turbo: [Turbopack] WebSocket connection to 'ws://localhost:3000/_next/webpack-hmr' failed

What version of Turbopack are you using?

default for next 13

What package manager are you using / does the bug impact?

pnpm

What operating system are you using?

Mac

Describe the Bug

Adding the --turbo on the package.json/scripts/dev value throws the error in the browser console.

Expected Behavior

shouldn’t throw any error in the browser console.

To Reproduce

  1. Create a monorepo with Turborepo.
  2. Locale the package.json within the web app.
  3. Add --turbo at the end of "dev": "next dev --turbo"
  4. Add a new folder named app
  5. Create a page.tsx (working with Javascript throws the same error)
  6. Add a layout.tsx file.
  7. run pnpm dev on the terminal.
  8. Open the Browser and go to http://localhost:3000.
  9. Open the devtools, the error should be printed like: image
  10. Removing the --turbo in the dev script, the browser console error goes away.

Reproduction Repo

https://github.com/blopez2010/my-turborepo

About this issue

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

Commits related to this issue

Most upvoted comments

Turbopack’s dev server is not compatible with the builds produced by webpack. You may have a cached JS file or something that is continuing to make requests.

https://github.com/vercel/turbo/blob/56039940cdfac07b68e030da1396214962e99ceb/crates/turbopack-dev-server/src/lib.rs#L200-L209

After a lot of investigation tonight, I’ve concluded that this issue is definitely caused by the use of PNPM.

I believe (I could be wrong, I don’t write Rust) the part of Turbopack that uses HMR to fetch and update code, doesn’t resolve symlinks - which is inadvertently causing the error code above.

I really don’t have a great suggestion at the moment to resolve the issue then switching back to NPM. You can use NPM just in the nextjs directory but you’ll need to watch out for differences in workspace package declarations.

On a side note I also discovered that Next.js is doing some work already to detect the use of turborepo by looking for turbo.json. I’m hopeful one of the library authors can give us an update about this one.

I got this yesterday when working with multiple next projects. I solved it by issuing a ‘killall node’ and then restarting.

It’s curious this bug doesn’t occur outside of a monorepo setup. I can reproduce it in an almost-empty turborepo:

  1. npx create-turbo@latest turbopack-bug
  2. Update next to canary
  3. Enable experimental.appDir: true in apps/web/next.config.js
  4. Delete apps/web/pages
  5. Add apps/web/app/page.tsx with export default function() { return <div>Hello</div>; }
  6. Run next dev --turbo
  7. Observe the bug:
Screenshot 2022-10-30 at 10 55 00 Screenshot 2022-10-30 at 10 55 14 Screenshot 2022-10-30 at 10 55 40