Parsing error: Cannot find module 'next/babel' [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 7, 2024
2 min

banner

# Parsing error: Cannot find module 'next/babel' [Solved]

To solve the "Parsing error: Cannot find module 'next/babel'" error:

  1. Create a .babelrc file in the root directory of your Next.js project (where your package.json file is) and add the following code to it.
.babelrc
{ "presets": ["next/babel"], "plugins": [] }

You can read more about customizing your Babel config in this section of the docs.

  1. Update the extends property in your .eslintrc file.
.eslintrc
{ "extends": ["next", "next/core-web-vitals", "prettier"] }

To install the eslint-config-prettier module, open your terminal in your project's root directory and run the following command.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install --save-dev eslint-config-prettier # ๐Ÿ‘‡๏ธ with YARN yarn add --dev eslint-config-prettier

install eslint config prettier

If the error is not resolved, try restarting your IDE and development server. VSCode often glitches and needs a reboot.

You can read more about customizing your ESLint config in this section of the docs.

The error also commonly occurs in Visual Studio Code projects that are split between multiple directories.

shell
frontend |---- package.json backend |---- package.json
To solve the error in this case, you could try to open Visual Studio Code in the frontend directory or the backend directory, instead of opening it in the folder that contains both frontend and backend.

Alternatively, you can create a .vscode folder in the root directory of your project and add a settings.json file to it.

shell
frontend |---- package.json backend |---- package.json .vscode |---- settings.json

In your .vscode/settings.json file, add the path to your working directories.

.vscode/settings.json
{ "eslint.workingDirectories": [ "./frontend", "./backend" ] }

Make sure to update the path if your working directories are not called frontend and backend.

vscode settings json file

Restart your IDE and development server after adding the .vscode/settings.json file.

# Delete your node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json (not package.json) files, re-run npm install and restart your IDE.

shell
# ๐Ÿ‘‡๏ธ (Windows) Delete node_modules and package-lock.json rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ (macOS/Linux) Delete node_modules and package-lock.json rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ Clean your npm cache npm cache clean --force npm install

Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and needs a reboot.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev