next.js: Not working with PNPM
Bug report
Describe the bug
I tried using Next.js with Rush.js which is a tool that helps developers create and maintain monorepo projects. It has support for NPM, Yarn and PNPM. I tried using with PNPM cause it’s really good compared to Yarn and NPM but I was surprised that it wasn’t working with Next.js. Next.js is working only with Yarn and NPM and it appears that Next.js’s server-renderer bundler is not correctly externalizing modules with PNPM.
To Reproduce
https://github.com/zefexdeveloper/nextjs-pnpm
You can clone the repository and follow the readme. There’s also a yarn
branch with a working example.
Expected behavior
It should work just like with Yarn/NPM.
Additional context
I would like to invite @octogonz (Rush.js), @timneutkens @Timer (Next.js) and @zkochan (PNPM). Great people that are related to those projects and it would be amazing to hear from them to see what might be the problem.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 14
- Comments: 21 (10 by maintainers)
Commits related to this issue
- ⬆️ [apps/wheel-api] Bump next.js Can not use Next.js 9.5+ because of https://github.com/vercel/next.js/issues/16471 — committed to vessel-kit/vessel-kit by ukstv 4 years ago
I see that PR https://github.com/vercel/next.js/pull/16614 was closed recently.
Is PR https://github.com/vercel/next.js/pull/16369 expected to fix this issue?
Just to add to data here: We’re running Next on a non-Vercel host, and our app also doesn’t work without use of the
--shamefully-hoist
.This issue should probably be reopened, or alternatively another issue cut. It feels like a fairly high-quality bug with easy reproduction and a couple canned projects that show the issue.
It seems like adding “–shamefully-hoist” in the install command fixes it
EDIT: DOESN’T WORK FOR Next.js 12 Serverless Functions in Vercel
EDIT2: IT WORKS AGAIN FOR Next.js 12.0.3 🎉
I also found an older PR https://github.com/vercel/next.js/pull/8668 where @arcanis fixed Next.js to work with Yarn Plug’n’Play. It is fortunately a pretty small diff. @zkochan I bet these are the exact same places that need to be fixed up to eliminate the incompatibility with PNPM. Maybe this is easy to solve!
From a preliminary investigation, we found that Next.js’s server-renderer bundler is not correctly externalizing modules installed by PNPM. For example, if we compare the file
.next\server\pages\index.js
between the Yarn vs PNPM branches from @zefexdeveloper’s repro:For Yarn we will see this:
.next\server\pages\index.js
…whereas for PNPM we see this:
.next\server\pages\index.js
…along with full copies of every NPM dependency, copied inline into every .js file. For example the same is seen in other files
.next\server\pages\_document.js
,.next\server\pages\_app.js
, etc.As a result, React reports a warning about hooks. The warning is complaining that multiple instances of React are getting loaded (into the Node.js server-side renderer, not the web browser runtime). Next.js seems to be using Webpack to bundle everything, so maybe it is just a configuration problem. Webpack itself certainly is compatible with PNPM.
The webpack configuration is here: packages/next/build/webpack-config.ts
It references packages/next/lib/resolve-request.ts, which looks to be doing custom module resolution. Perhaps it does not handle symlinks correctly according to the Node.js standard.
Thank you @bitfrost for the PR, I hope that @Timer or @timneutkens can merge.
Dude, thanks so much. Literally save my bacon!!!