Parsing error: Cannot find module 'next/babel' in Visual Studio Code
See original GitHub issueWhat version of Turborepo are you using?
1.4.7
What package manager are you using / does the bug impact?
pnpm
What operating system are you using?
Mac
Describe the Bug
Hi! I just created a turborepo project using npx
, and VSCode throws the following error:
Parsing error: Cannot find module 'next/babel'
Require stack:
- /Users/celia/.dev/flex/node_modules/.pnpm/next@12.3.0_ir3quccc6i62x6qn6jjhyjjiey/node_modules/next/dist/compiled/babel/bundle.js
- /Users/celia/.dev/flex/node_modules/.pnpm/next@12.3.0_ir3quccc6i62x6qn6jjhyjjiey/node_modules/next/dist/compiled/babel/eslint-parser.js
- /Users/celia/.dev/flex/node_modules/.pnpm/eslint-config-next@12.3.0_dyxdave6dwjbccc5dgiifcmuza/node_modules/eslint-config-next/parser.js
- /Users/celia/.dev/flex/node_modules/.pnpm/@eslint+eslintrc@0.4.3/node_modules/@eslint/eslintrc/lib/config-array-factory.js
- /Users/celia/.dev/flex/node_modules/.pnpm/@eslint+eslintrc@0.4.3/node_modules/@eslint/eslintrc/lib/index.js
- /Users/celia/.dev/flex/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/cli-engine/cli-engine.js
- /Users/celia/.dev/flex/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/cli-engine/index.js
- /Users/celia/.dev/flex/node_modules/.pnpm/eslint@7.32.0/node_modules/eslint/lib/api.js
- /Users/celia/.vscode-insiders/extensions/dbaeumer.vscode-eslint-2.2.6/server/out/eslintServer.jseslint
This is from a project out-of-the-box, created with npx create-turbo
. I haven’t made any changes to the bootstrapped files.
I have tried a few solutions:
- This workaround suggested by Saral Karki: basically adding
"next/babel"
in the list of extends, inside ofeslint-config-custom
. It does “fix” the error inside VSCode, making it stop yelling at me. - However, with this change,
pnpm run lint
breaks throwing the following error:Failed to load config "next/babel" to extend from.
. I then found this thread, where Domi said the first fix I tried is basically a “hack”. I then tried tinkering around with Akasha’s answer, with no success. - Setting up
eslint.workingDirectories
to mypackages/
andapps/
folders in my.vscode/settings.json
(making sure I had no conflict in my personalsettings.json
, no success here either. - Setting up
eslint.packageManager
topnpm
, nothing here. - Installing
@babel/eslint-parser
, nothing either.
Note: the issue does not appear when using npm
or yarn
as the default package manager, configured in the create-turbo
CLI.
Expected Behavior
VSCode doesn’t throw an error about a parsing error, and the build runs correctly.
To Reproduce
- Run
pnpx create-turbo
ornpx create-turbo
- Setup using CLI as normal, and select
pnpm
as the package manager. - Open Visual Studio Code with the ESLint extension code installed.
- Open any
.eslintrc.js
or anynext.config.js
file in the default project. - The error will be visible at the top of the IDE window.
Issue Analytics
- State:
- Created a year ago
- Reactions:10
- Comments:13 (3 by maintainers)
Top Results From Across the Web
Parsing error : Cannot find module 'next/babel' - Stack Overflow
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...
Read more >Parsing error: Cannot find module 'next/babel' [Solved]
To solve the error in this case, you could try to open Visual Studio Code in the frontend directory or the backend directory,...
Read more >Cannot find module 'next/babel' on a new Next.js project - Reddit
Parsing error : Cannot find module 'next/babel' on a new Next.js project. I saw the same question on Stack Overflow: https://stackoverflow.
Read more >How to Fix “Parsing error: Cannot find module 'next/babel'” in ...
Create file called .babelrc in your root directory and add this code { "presets": ["next/babel"], "plugins": [] } And in .eslintrc replace ...
Read more >Parsing error : Cannot find module 'next/babel' - Code Grepper
parsing error : cannot find module 'next/babel' ; 4. Tip Adika King 1 GREPCC · // Add the following code in .eslintrc, replace...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I was able to get this working also by installing
next
in the root of my pnpm workspace. I was curious to see if there was a better way and I managed to come up with a workaround I haven’t seen yet, maybe it would be useful to someone else.I’m not sure where the above mentioned line comes into play, but I found the problematic part of the config for me was this line:
https://github.com/vercel/next.js/blob/cae96f27ec53cd42e992a748b9727852df9247c5/packages/eslint-config-next/index.js#L85
Which makes total sense because I only experience this issue in certain js config files, not in typescript because that uses the typescript parser rather than the babel one.
In order to workaround this, I installed
next
in the eslint config package within my workspace and I usedrequire.resolve
to supply the babel parser with an absolute path tonext/babel
within my sharable config. It looks like this:So far, I haven’t found any unintended side-effects. I still have some testing to do in order to be certain.
I think I understand. The discussions here have all been focused around monorepo or multi-package repo setup. Are you currently using a package manager to leverage this functionality? All the major players have support now, but I use
pnpm
. When I refer to root, I am saying at the very base directory of my project, there is a file calledpnpm-workspace.yaml
and this tells me where I have subpackages or projects installed. In yarn or npm this will beworkspaces
property in package.json. If you are not using these features, you might check it out to see if it would be good for your way of doing things.The reason why it works correctly when you open a project in VSCode directly is because often times extensions use default install locations and since you have an unusual layout, VSCode just gets confused. The ESLint setting “eslint.workingDirectories” is an important setting for this type of scenario. One of my next projects is setup like this:
This allows each project in apps/ or packages/ to supply their own linting config and the extension should read from the config of the project you’re in. Hopefully that helps!