Search code examples
typescriptbuildnext.jsbuild-error

NextJS cannot find module constants.js on build


I have this error when running next build with next version ^10.2.3. I tried to delete node_modules and .next folder and fresh running npm install && next build without success.

  Error: Cannot find module 'next/dist/shared/lib/constants.js'
  type: 'Error',
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '.next/server/pages/_document.js',
    '/usr/local/lib/node_modules/next/dist/server/require.js',
    '/usr/local/lib/node_modules/next/dist/server/load-components.js',
    '/usr/local/lib/node_modules/next/dist/build/utils.js',
    '/usr/local/lib/node_modules/next/dist/build/worker.js',
    '/usr/local/lib/node_modules/next/dist/compiled/jest-worker/processChild.js'
  ]

Solution

  • Clear the npm cache by running the following command in your project's root directory:

    npm cache clean --force
    

    Delete the package-lock.json file and the node_modules folder.

    Reinstall the dependencies by running:

    npm install
    

    Restart your Next.js development server:

    npm run dev
    

    also remove the .next folder