Next.js: Set the node_modules as symlink in 9.5.2, the build process fails

Created on 18 Aug 2020  路  4Comments  路  Source: vercel/next.js

Bug report

Describe the bug

Set the node_modules as symlink, in v9.5.2, the build process fail while v9.4.4 is success.

The error messages:

yarn run v1.22.4
$ next build
info  - Creating an optimized production build
info  - Compiled successfully
info  - Collecting page data
[=   ] info  - Generating static pages (0/2)
Error occurred prerendering page "/404". Read more: https://err.sh/next.js/prerender-error
Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at Z (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:177:404)
    at module.exports.BdB7.exports.useContext (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:182:261)
    at Html (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:773:29)
    at d (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:498)
    at $a (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:39:16)
    at a.b.render (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:476)
    at a.b.read (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
    at renderToStaticMarkup (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:462)
    at renderDocument (/Users/Hank.Lin/playground/node_modules/next/dist/next-server/server/render.js:3:624)
    at renderToHTML (/Users/Hank.Lin/playground/node_modules/next/dist/next-server/server/render.js:50:72)

Error occurred prerendering page "/". Read more: https://err.sh/next.js/prerender-error
Error: Minified React error #321; visit https://reactjs.org/docs/error-decoder.html?invariant=321 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
    at Z (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:177:404)
    at module.exports.BdB7.exports.useContext (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:182:261)
    at Html (/Users/Hank.Lin/playground/next-app/.next/server/pages/_document.js:773:29)
    at d (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:498)
    at $a (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:39:16)
    at a.b.render (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:476)
    at a.b.read (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
    at renderToStaticMarkup (/Users/Hank.Lin/playground/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:462)
    at renderDocument (/Users/Hank.Lin/playground/node_modules/next/dist/next-server/server/render.js:3:624)
    at renderToHTML (/Users/Hank.Lin/playground/node_modules/next/dist/next-server/server/render.js:50:72)
info  - Generating static pages (2/2)

> Build error occurred
Error: Export encountered errors on following paths:
    /
    /404
    at exportApp (/Users/Hank.Lin/playground/node_modules/next/dist/export/index.js:24:1103)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async build (/Users/Hank.Lin/playground/node_modules/next/dist/build/index.js:37:212)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

To Reproduce

  1. yarn create next-app
  2. rm -rf node_modules & ln -s some_path/node_modules
  3. yarn & yarn build

System information

  • OS: mac os
  • Version of Next.js: 9.5.2
  • Version of Node.js: 12.16.1
good first issue bug

All 4 comments

Same here, but the error is different.

image

For the production build is success, but when starting the service i got this error.

Feel free to investigate the issue @jyunhanlin

In next.js/packages/next/pages/_document.tsx, the useContext in Html and Main component is breaking the Rules of Hooks when set the node_modules as symlink. (also happened in 9.5.3)

Investigation update:

When set the node_modules as symlink, and the run yarn build, the .next/server/pages/_document.js contained the react.producion.min.js

Refer to https://github.com/facebook/react/issues/16029#issuecomment-570912067
Set the webpack config in next.confg.js

config.externals = [
  ...config.externals,
  {
    react: {
      root: 'React',
      commonjs2: 'react',
      commonjs: 'react',
      amd: 'react'
    },
    'react-dom': {
      root: 'ReactDOM',
      commonjs2: 'react-dom',
      commonjs: 'react-dom',
      amd: 'react-dom'
    }
  }
]

The build process is success.

The next.js version is 9.5.4-canary.5

Was this page helpful?
0 / 5 - 0 ratings