Next.js cannot run in Yarn v2 PnP mode with "enableGlobalCache" enabled.
Execute following shell script.
mkdir pages
cat <<'EOF' > pages/index.js
export default function Index() {
return <h1>Hi</h1>
}
EOF
cat <<'EOF' > package.json
{
"dependencies": {
"next": "9.5.1",
"react": "16.13.1",
"react-dom": "16.13.1"
}
}
EOF
yarn set version berry
yarn config set enableGlobalCache true
yarn install
yarn next dev
Next.js server should be started.

error - ./.yarn/$$virtual/next-virtual-061300f485/3/.yarn/berry/cache/next-npm-9.4.5-canary.39-f06383efbf-5.zip/node_modules/next/dist/client/dev/amp-dev.js
Error: [BABEL] /home/simnalamburt/workspace/a/.yarn/$$virtual/next-virtual-061300f485/3/.yarn/berry/cache/next-npm-9.4.5-canary.39-f06383efbf-5.zip/node_modules/next/dist/client/dev/amp-dev.js: Failed to resolve "@babel/runtime" relative to "/home/simnalamburt/workspace/a/.yarn/$$virtual/next-virtual-061300f485/3/.yarn/berry/cache/next-npm-9.4.5-canary.39-f06383efbf-5.zip/node_modules/next/dist/build/babel" (While processing: "programmatic item$6")
at Array.reduce (<anonymous>)
9.4.4, 9.4.5-canary.31, 9.4.5-canary.39, 9.5.1It seems to be a problem with some of the dependencies used by Next.js. I want to know which dependencies cause problems.
I also encountered the same issue in version 9.5.1. Managed to workaround it by applying the following patch to @babel/plugin-transform-runtime.
diff --git a/lib/get-runtime-path/index.js b/lib/get-runtime-path/index.js
index 354208c9eaaec10e5067156c004ff7a32fcfa921..483a5bd2b5ee17b65719c7fa3934e1c694d22e8b 100644
--- a/lib/get-runtime-path/index.js
+++ b/lib/get-runtime-path/index.js
@@ -18,9 +18,17 @@ function _default(moduleName, dirname, absoluteRuntime) {
function resolveAbsoluteRuntime(moduleName, dirname) {
try {
- return _path.default.dirname(_resolve.default.sync(`${moduleName}/package.json`, {
- basedir: dirname
- })).replace(/\\/g, "/");
+ var pkg;
+ if (process.versions.pnp) {
+ pkg = require("pnpapi").resolveRequest(`${moduleName}/package.json`, dirname, {
+ considerBuiltins: false
+ });
+ } else {
+ pkg = _resolve.default.sync(`${moduleName}/package.json`, {
+ basedir: dirname
+ });
+ }
+ return _path.default.dirname(pkg).replace(/\\/g, "/");
} catch (err) {
if (err.code !== "MODULE_NOT_FOUND") throw err;
throw Object.assign(new Error(`Failed to resolve "${moduleName}" relative to "${dirname}"`), {
And adding this to package.json:
"resolutions": {
"next/@babel/plugin-transform-runtime": "patch:@babel/[email protected]#./transform-runtime.patch"
}
@jdmota Thanks for the awesome patch! I'll try to send a PR to the babel repo
@simnalamburt No problem! But I think this is more related to the resolve package. For some reason, the PnP Api is not getting injected...
@arcanis, do you know what the issue is? Thanks!
Resolved in Next.js 9.5.5 and Yarn 2.3.2. Thanks!
Most helpful comment
@simnalamburt No problem! But I think this is more related to the
resolvepackage. For some reason, the PnP Api is not getting injected...@arcanis, do you know what the issue is? Thanks!