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:
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies. The module you're trying to import is in a different directory. The module you're trying to import has a different casing.
Next. js includes the next/babel preset to your app, which includes everything needed to compile React applications and server-side code.
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"]
}
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.
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:
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"
]
}
In your NextJS Project you have this 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: You don't need to create extra .babelrc file
Note: If you only replace with
{
"extends": ["next/babel"]
}
The red error sign will go but the code won't compile and gives compile error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With