212

Update 1: Updated the latest working solution to @Jeevan Rupacha answer, please scroll below to check it out.

I have been encountering this error on every single new Next.js project that I create. The page can be compiled without any problem, it just keeps on showing as error on the first line in every js file.

Parsing error: Cannot find module 'next/babel' Require stack:

  • D:\app\next_app\ratu-seo\node_modules\next\dist\compiled\babel\bundle.js
  • D:\app\next_app\ratu-seo\node_modules\next\dist\compiled\babel\eslint-parser.js
  • D:\app\next_app\ratu-seo\node_modules\eslint-config-next\parser.js
  • D:\app\next_app\ratu-seo\node_modules@eslint\eslintrc\lib\config-array-factory.js
  • D:\app\next_app\ratu-seo\node_modules@eslint\eslintrc\lib\index.js
  • D:\app\next_app\ratu-seo\node_modules\eslint\lib\cli-engine\cli-engine.js
  • D:\app\next_app\ratu-seo\node_modules\eslint\lib\cli-engine\index.js
  • D:\app\next_app\ratu-seo\node_modules\eslint\lib\api.js
  • c:\Users\Admin.vscode\extensions\dbaeumer.vscode-eslint-2.1.23\server\out\eslintServer.js
3
  • Do you have a .babelrc file? And what version of Next.js are you on? Commented Jun 29, 2021 at 17:12
  • 2
    I can't find the .babelrc file. And im on Next.js v11.0.1. Is it normal that the babelrc file is missing? I generated the whole project with npx create-next-app Commented Jun 30, 2021 at 10:28
  • @mitchiri_neko Yes, next uses swc to compile. Commented Feb 14, 2023 at 19:22

25 Answers 25

513

Create file called .babelrc in your root directory and add this code:

{
  "presets": ["next/babel"],
  "plugins": []
}

And in .eslintrc, replace the existing code with:

{
  "extends": ["next/babel","next/core-web-vitals"]
}
Sign up to request clarification or add additional context in comments.

16 Comments

as soon as I replace the eslintrc extends, its gone! Big thanks!
What are the possible side effects of this?
Thanks! For some reason I got the errors even on the initial install using create-next-app. This fixes it!
I tried this but weirdly eslint just stopped working. Do you know any causes of this?
Sounds outdated answer since SWC is now the default. I have this issue in a v13 app, very weird
|
406

You don't need to create an extra .babelrc file.

In your NextJS project you have a file named .eslintrc.json. In this file you have following code:

{
  "extends": "next/core-web-vitals"
}

Replace it with

{
  "extends": ["next/babel","next/core-web-vitals"]
}

Note: If you only replace with

{
   "extends": ["next/babel"]
}

The red error sign will appear but the code won't compile and gives compile error.

`Note: If ["next/babel"] is not working try only ["next"]

7 Comments

Thx! and agreed, should be at the top. adding a .babelrc file will disable SWC in NextJS v12
why they have not made this the default?
You may need to restart ESLint server -> <cmd/ctrl + shift + p> then start typing Restart ESLint Server
This is an older fix, no instead of "next/babel" should be "next"
|
156

I had this same problem - but only when I wasn't opening the project folder directly. It appears to be related to how ESLint needs to be configured for workspaces.

In addition, the currently accepted answer works for VSCode but breaks npm run lint for me.

TL;DR - see this answer which points to this blog. This fixed it for me.

Some Details

For example, if I have:

~
|  -- some_folder
|     | -- project_1
|     | -- project_2
|     ...files relating to both projects...

I'll often just cd ~/some_folder && code .

But then I get the same error you're experiencing. However, if I cd ~/some_folder/project_1 && code . then everything works fine.

If that's the case for you as well, then what you need (as described in the links above) is to:

  • Create a workspace config
  • Specify folders where you need ESLint to run

You can do this easily from VSCode. The net result (following my example above) is a file named ~/some_folder/.vscode/settings.json with contents

{
    "eslint.workingDirectories": [
        "./project_1",
        "./project_2"
    ]
}

8 Comments

This fixed it for me. I had a monorepository with two different eslint configs that influenced each other. By creating the workspace config as described above the issue was gone.
This answer is pretty important! It fixed for me the errors. Still, the esLint config was way not enough just using next core web vitals. I ended up with these for now: extends: ['eslint:recommended', 'plugin:import/errors', 'plugin:import/warnings', 'plugin:react/recommended', 'next/core-web-vitals','prettier' ],
I had the same issue in my monorepo setup and I believe this is the actual root cause solution, thanks! We shouldn't need to add anything to .eslintrc, and we especially shouldn't need to add a .babelrc since Next.js includes next/babel preset in the app.
Had the same problem in a monorepo. Adding vscode settings.json fixed it; thanks for taking the time to share!
seemed to work for me
|
25

For Nextjs 12 add prettier in .eslintrc.json file inside your root folder.

{
  "extends": ["next/core-web-vitals", "prettier"]
}

5 Comments

This is the correct answer for me. Babel is not a dependency of my NextJS project, and this was a result of prettier extensions. Adding unneeded babel configs or eslint extensions referencing babel is a redherring.
I've tried disabling the prettier extension but it did not suffice, I had to apply this fix... Still a better answer than the other since Next.js doesn't use Babel as a default anymore.
this worked for me in latest nextjs version. Using VSCode
For the people who is using prettier with newly created project and get the error, this solution the best one I guess. Thanks Mel.
Yes. This worked for me too. Thank you
18

For me, it's work with editing this file in NextJS 13 in app version: .eslintrc.json

{
  "extends": ["next/babel", "next/core-web-vitals"]
}

1 Comment

That is the real solution
15

Using Next.js, TypeScript, and Tailwind CSS, I updated the .eslintrc.json file with:

{      
  "extends": ["next/babel", "next/core-web-vitals"]
}

then ctrl + shift + p and search for ESLint: Restart ESLint Server.

Comments

15

In my case, the issue occurs when I code in VSCode and use pnpm as the package manager, I tried lots of methods in StackOverflow, and finally, I find out that it's the duty of the VSCode ESLint plugin.

So, to fix the problem without uninstalling the plugin, add the following configuration in the .vscode/settings.json and reload your editor.

{
  "eslint.packageManager": "pnpm"
}

1 Comment

unfortunately this option is deprecated, the only working solution for me was disable the plugin and just leverage on the turborepo setup
11

It worked for me by just adding prettier in .eslintrc file.

{
  "extends": ["next", "prettier"]
}

Comments

10

Try updating the .eslintrc.json file to

{
  "extends": ["next", "prettier","next/core-web-vitals"],
  "plugins": ["prettier"]
}

Also install prettier plugin if you don't have it already

npm install eslint-plugin-prettier@latest --save-dev

Don't have to include .babelrc file as it will replace Nextjs SWC compiler S

1 Comment

This did it for me!
9

My Problem

I found this error in a project with PNPM, ESLint, and Monorepo architecture using Turborepo.

⚠️ WARNING!

UPDATE 27-8-2023: I created a new turborepo project and encountered this problem again, I found out, that implementing this solution will silently disable the entire ESLint system.

Please be careful! I recommend not using this solution anymore!

My Solution

add this into the ESLint config file:

parserOptions: {
    babelOptions: {
      presets: [require.resolve('next/babel')],
    },
  },

10 Comments

This solved my issue as well, I have the same setup pnpm, turbo. Thanks for this!
This disabled linting in VSCode unless I installed Next.js to the project with the eslint config.
@DonovanDikaio I have updated the turbo examples with this fix, so anyone in the future shouldn't encounter this error.
I am having this problem in VSCode with the Turborepo with-tailwind example, despite the example including the above configuration -- but only within VSCode.
This worked like a charm in my pnpm monorepo after trying a lot of other stuff.
|
6

I had this same problem - Close the IDE and reopen it with the project folder !!

Comments

4

If you are using vscode and have multiple folders for your project create .vscode folder in the root of the project and create setting.json file and add following configuration.

 {
  "eslint.workingDirectories": [
   "./client",
   "./server"
   ] 
 }

Make sure to include the right folder names of your projects

1 Comment

In my case, I have a vscode workspace. ` { "folders": [ { "path": "." } ], "settings": { "eslint.workingDirectories": ["./server", "./admin"] } } `
3

This is still an issue with NextJS 13.4.2. This is the best fix I've found:

{
  // You might want to additionally set this in monorepos where Next.js app is in a subdir
  "root": true,
  "extends": ["next/core-web-vitals"],
  "overrides": [
    {
      // Adapt to your needs (e.g. some might want to only override "next.config.js")
      "files": ["*.js"],
      // This is the default parser of ESLint
      "parser": "espree",
      "parserOptions": {
        "ecmaVersion": 2020
      }
    }
  ]
}

Source: Issues tab from the NextJS repo on Github.

2 Comments

This works for me. Tried everything. I had to use "files": [".js",".mjs"] because I'm working with modules.
I went with files: ["*.js", "*.jsx", "*.[c|m]js"] as the config was complaining when linting .eslintrc.cjs. I assumed it would be the same for all JS files, so added .jsx just in case.
3

Actually this issues happens for installing eslint and next/core-web-vitals creates conflicts with the parser, just update the eslint config for parser.

  "parser": "espree",
  "parserOptions": {
    "ecmaVersion": 2020
  }

Alternatively update the config with

  "extends": ["next"],

Comments

3

If your project builds fine but are only getting the Cannot find module 'next/babel' error in vscode on line 1 and your eslintrc file is not at the root level of your workspace then simply...

  1. copy/cut and paste your eslintrc file to the root of your workspace
  2. close vscode
  3. reopen vscode

Comments

1

You can also always try updating React and then Next. I had the same error message then updated them both and now my app is working fine.

Upgrade React version to latest Most applications already use the latest version of React, with Next.js 11 the minimum React version has been updated to 17.0.2.

To upgrade you can run the following command:

npm install react@latest react-dom@latest

Or using yarn:

yarn add react@latest react-dom@latest

Upgrade Next.js version to latest To upgrade you can run the following command in the terminal:

npm install next@latest

or

yarn add next@latest

Comments

1

Really, just adding prettier to "extends": ["next/core-web-vitals] to have something like ==> {"extends": ["next/core-web-vitals", "prettier"]}, gets rid of the error, without having to create an extra .babelrc file. But another research that needs to be done, is to know, if there are any downsides to doing it, and i think the answer is NO

Comments

1

In my case, the problem is that I added "eslint.packageManager": "yarn" to the setting.json of VSCode before, and I tried to use the same setup within a new project managed with pnpm. After adding "eslint.packageManager": "pnpm" for the new workspace, and reload window, than the issue's gone.

I've tried adding "extends": ["next/babel", "next/core-web-vitals", "prettier"] to .eslintrc.js, it will fix the error only within VSCode, but will cause the other error when building my Next.js app.

Comments

1

Just had this issue with the Nextjs app and the following worked for me. I no longer have issue with next/babel and I can run yarn lint.

Add prettier to your project

yarn add --dev eslint-config-prettier

Update your eslint config as follows

{
  "extends": ["prettier", "next/core-web-vitals"]
}

Add global vscode settings and include your project path

{
  "eslint.workingDirectories": ["./your-project"]
}

Comments

1

Interesting we have answers for two separate issues here:

  • OP's issue was with Nextjs 11 where the solution was adding to the eslint config
    • It's hard to say what OP's root cause was without knowing if it was a monorepo, but adding a .babelrc was certainly not the solution since Next.js includes the next/babel preset in the app by default. It may have been related to Prettier since Next.js has some suggestions about using it with eslint. Adding "prettier" to my extends did make the error go away on my end, but that wasn't the root cause in my case.
  • Others including me ran into the same message with Nextjs 13, this time in a monorepo setup.
    • The root cause solution was as described in this answer once I set eslint.workingDirectories with each of my packages the error went away for good

Comments

0

In my case I had to disable VSCode ESLint extension.

Unfortunately when I added ["next/babel"] in extends, the npm run lint stopped working and Eslint in vscode did not underlining any abnormalities.

1 Comment

If you disable vs code eslint extension then it wouldn't underline abnormalities
0

ctrl + shift + p

TypeScript: Restart TS server

Comments

0

In my project, i just run yarn add @babel/core and run ctrl + shift + p in vscode, excute ESLint: Restart ESlint Server

ESLint: Restart ESlint Server

it works, and npm run lint works fine too.

> Executing task: yarn run lint <

✔ No ESLint warnings or errors

Comments

0

My Working Solution: uninstal vscode-eslint plugin

after longs and painfully hours struggling with this issue, trying every suggested working solution I found the only real one: remove the eslint plugin from vscode and just leverage on the turborepo configuration

Comments

0

You need to install next in eslint-config-custom to make next/babel available

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.