skip to Main Content

So I recently update a Next project from Next 12 to 14, and am running into a ton of import errors when trying to run the project locally. There are too many for me to copy and paste them all, so here are some examples:

Import trace for requested module:
./components/Header/Header.tsx

./components/Header/Header.tsx
Attempted import error: 'react' does not contain a default export (imported as 'React').

Import trace for requested module:
./components/Header/Header.tsx

./components/Header/Header.tsx
Attempted import error: 'react' does not contain a default export (imported as 'React').

Import trace for requested module:
./components/Header/Header.tsx

./components/Logo/Logo.tsx
Attempted import error: 'react' does not contain a default export (imported as 'React').

Import trace for requested module:
./components/Logo/Logo.tsx
./components/Nav/Nav.tsx

./components/Nav/Nav.tsx
Attempted import error: 'useState' is not exported from 'react' (imported as 'useState').

Import trace for requested module:
./components/Nav/Nav.tsx

./components/Nav/Nav.tsx
Attempted import error: 'useEffect' is not exported from 'react' (imported as 'useEffect').

./node_modules/react-transition-group/esm/utils/ChildMapping.js
Attempted import error: 'cloneElement' is not exported from 'react' (imported as 'cloneElement').

Import trace for requested module:
./node_modules/react-transition-group/esm/utils/ChildMapping.js
./node_modules/react-transition-group/esm/TransitionGroup.js
./node_modules/react-transition-group/esm/index.js
./node_modules/@mui/material/ButtonBase/TouchRipple.js
./node_modules/@mui/material/ButtonBase/ButtonBase.js
./node_modules/@mui/material/ButtonBase/index.js
./node_modules/@mui/material/Button/Button.js
./node_modules/@mui/material/Button/index.js
./components/Header/Header.tsx

./node_modules/react-transition-group/esm/utils/ChildMapping.js
Attempted import error: 'isValidElement' is not exported from 'react' (imported as 'isValidElement').

I tried deleting node_modules and package-lock.json and reinstalling and haven’t found the solution.

Here is my tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "types": [
      "jest",
      "node"
    ],
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    "moduleResolution": "node",
    "module": "esnext"
  },
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

My next.config.cjs:

/** @type {import('next').NextConfig} */
const nextConfig = {}

module.exports = nextConfig

And my package.json, if that helps:

{
  "name": "benedict-construction",
  "type": "module",
  "version": "0.1.0",
  "private": true,
  "test": "jest --watchAll=false",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "test": "jest"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "dependencies": {
    "@emotion/react": "^11.11.3",
    "@emotion/styled": "^11.11.0",
    "@mui/icons-material": "^5.15.6",
    "@mui/material": "^5.15.6",
    "@types/node": "20.5.4",
    "autoprefixer": "10.4.15",
    "next": "^14.1.0",
    "postcss": "8.4.28",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "tailwindcss": "3.3.3"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.22.20",
    "@babel/preset-react": "^7.23.3",
    "@babel/preset-typescript": "^7.23.0",
    "@jest/globals": "^29.7.0",
    "@testing-library/dom": "^9.3.1",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^14.0.0",
    "@testing-library/user-event": "^14.4.3",
    "@types/jest": "^29.5.5",
    "@types/react": "^18.2.48",
    "@types/react-dom": "^18.2.18",
    "@types/testing-library__jest-dom": "^6.0.0",
    "add": "^2.0.6",
    "babel-jest": "^29.7.0",
    "encoding": "^0.1.13",
    "eslint": "^8.50.0",
    "eslint-config-next": "^14.1.0",
    "eslint-config-react-app": "^7.0.1",
    "eslint-plugin-material-ui": "^1.0.1",
    "eslint-plugin-react": "^7.33.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "jest": "^29.7.0",
    "jest-environment-jsdom": "^29.7.0",
    "ts-jest": "^29.1.1",
    "ts-node": "^10.9.1",
    "typescript": "^5.2.2"
  }
}

Any help would be greatly appreciate, thanks!

EDIT:

I am also seeing this in the browser console:

react-refresh-runtime.development.js:531 Uncaught ReferenceError: exports is not defined
    at eval (react-refresh-runtime.development.js:531:9)
    at eval (react-refresh-runtime.development.js:543:7)
    at (:3000/app-pages-browser)/node_modules/next/dist/compiled/react-refresh/cjs/react-refresh-runtime.development.js (http://localhost:3000/_next/static/chunks/main-app.js?v=1706986671318:127:1)
    at options.factory (webpack.js?v=1706986671318:722:31)
    at __webpack_require__ (webpack.js?v=1706986671318:37:33)
    at fn (webpack.js?v=1706986671318:377:21)
    at eval (runtime.js:3:22)
    at (:3000/app-pages-browser)/node_modules/next/dist/compiled/react-refresh/runtime.js (http://localhost:3000/_next/static/chunks/main-app.js?v=1706986671318:138:1)
    at options.factory (webpack.js?v=1706986671318:722:31)
    at __webpack_require__ (webpack.js?v=1706986671318:37:33)

2

Answers


  1. You no longer need to import react in your components, so remove anything like this:

    import React from 'react';
    or
    import 'react';

    You can leave any non-default import in such as:

    import { useEffect } from 'React';

    Login or Signup to reply.
  2. Id suggest migrating to the app router and following these two guides in order.
    Upgrade guide next 13
    Upgrade guide next 14

    Nextjs 13 changed to the app router. This means that the path for the import alias ‘@’ will be wrong in your tsconfig.json.

    After you have migrated to the app router try changing the following and see if it is working.

    Instead of your tsconfig.json:

    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    

    Try this:

    "paths": {
      "@/*": [
        "./app/*"
      ]
    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search