-
Notifications
You must be signed in to change notification settings - Fork 28k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing error: Cannot find module 'next/babel' in Visual Studio Code #40687
Comments
The root problem in this stack is that one of these four modules:
In one of these files:
Something depends on I'm going to transfer this issue over to Next.js for more help! |
Based on this line:
I believe |
@balazsorban44 More details: npm/cli#4958 |
My not-so-optimal workaround for this has been installing Next on the root workspace, i.e., |
I was able to get this working also by installing 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:
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 /**
* @type {import('eslint').Linter.Config}
*/
module.exports = {
extends: ['next/core-web-vitals', 'turbo', 'prettier'],
ignorePatterns: ['node_modules', 'dist'],
parserOptions: {
babelOptions: {
presets: [require.resolve('next/babel')],
},
},
}; So far, I haven't found any unintended side-effects. I still have some testing to do in order to be certain. |
I have been dealing with this issue off and on as well for months. I have a main 'react' folder with a lot of projects in that I like to have open as workspace so I can easily dig up solutions from other projects wh ilst working. Whenever I setup a new Next JS (npx create-next-app) with the default linting settings I ALWAYS have this issue and I have yet to find a workable solution. If I open just one project in VS Code then it is fine. Also if I remove Next's default linting and use say, Airbnb then that also doesn't throw this error. The issue clearly stems from having a root folder with multiple different react projects open as workspace in VS Code. If anyone gets a working solution then I would love to know. For now its easy enough to just remove the default linting setup from each new Next project and replace it with my customized Airbnb one. EDIT: To piggyback @zmrl010 comments above, I also dug into next.js/packages/eslint-config-next/index.js and just simply commented out |
I'm not sure if you misunderstood my comment, or if I am misunderstanding yours. With Removing the So there is my approach I outlined here, or the one found above that was to install |
This helped, thanks 🙏 |
@zmrl010 So yes there may be some confusion. I was just wondering, so the error comes up because as I think I mentioned, I have a main folder on my computer called 'react' which has EVERY react project that I have started. So lots and LOTS of projects. This main react folder is saved as a workspace in VS Code as I often like to work with easy access to lots of projects so I can find solutions for problems in a new project in older code bases. The ROOT of this project ('/react') doesn't have anything in it. Are you suggesting I install 'next' somewhere in this root folder? Would that solve the error? My Next Js projects live in /react/nextjs/some_project folder, so having NextJs installed into react would solve the eslint error? As I mentioned, it doesn't occur when I have JUST the project open on its own in VS Code, and it doesn't occur if I use other linting presets... |
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 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: // .vscode/settings.json
{
"eslint.workingDirectories": [
{ "pattern": "apps/*/" },
{ "pattern": "packages/*/" }
]
} 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! |
Note: I did try using eslint.workingDirectories with pnpm, which did not fix the issue (cf. the original message). |
@zmrl010 Well I think there was maybe some confusion going on, but I sort of followed your advice and ran pnpm init in my top level react folder, and then installed Next into this folder (even though it really does not need be here) and voila, the errors are all gone..... |
If you install the project with |
A somehow cleaner fix for this is to not let ESLint use the {
// 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
}
}
]
} |
Hi all ☀️, In @jankaifer I can make a PR if you think the fix is right. Let me know 🙏 |
The SO post looks solid, but I'm not able to reproduce the issue. |
As mentioned by the author this problem occurs when creating the project with npx create-turbo@latest |
This workaround isn't effective in all cases - just look at the full thread on Stack Overflow. That's why I came up with #40687 (comment) which is a bit more sensible workaround in my opinion. Ultimately, I think we should tackle the problem at source (I feel like #40687 (comment) & #40687 (comment) are going into the right direction) instead of applying some pseudo workarounds. |
Thanks for pointing out the issues with the SO solution I tried the second solution in that SO thread: add |
So this seems to be an issue with But it will be probably good to open a relevant issue with |
@zmrl010 Gonna ask the dumbest question here. Did you change your vscode |
My ProblemI found this error in project with PNPM, ESLint, and Monorepo architecture using Turborepo. My Solutionadd this into the ESLint config file: parserOptions: {
babelOptions: {
presets: [require.resolve('next/babel')],
},
}, |
in all (.eslintrc.js) files ?? |
@Learning-X I'm using a Monorepo architecture, so I only need to implement it in the parent or the main ESLint Config file. |
Not dumb at all, relative paths can be ambiguous. In my example above, I am referring to the settings file
As @ImBIOS said, you only need to do this once in the main eslint config that you extend. I've only done this in a monorepo setup with a separate If that sounds hacky, its because it is. I would opt for a different solution if you can find something, but this works as a bandaid fix. |
IMO, this is the cleanest solution with no hacks:
|
Just wanted to point out that this requires a VS Code restart to take effect. Worked great for me. |
@DavNej, Thanks for the suggestion. Further issue not observed. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What 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: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:
"next/babel"
in the list of extends, inside ofeslint-config-custom
. It does "fix" the error inside VSCode, making it stop yelling at me.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.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.eslint.packageManager
topnpm
, nothing here.@babel/eslint-parser
, nothing either.Note: the issue does not appear when using
npm
oryarn
as the default package manager, configured in thecreate-turbo
CLI.Expected Behavior
VSCode doesn't throw an error about a parsing error, and the build runs correctly.
To Reproduce
pnpx create-turbo
ornpx create-turbo
pnpm
as the package manager..eslintrc.js
or anynext.config.js
file in the default project.The text was updated successfully, but these errors were encountered: