typescript-eslint: v6: Cannot find module '@typescript-eslint/*' or its corresponding type declarations
Overview
A handful of folks have mentioned that after upgrading to v6, trying to import from @typescript-eslint/* packages such as @typescript-eslint/utils causes a new TypeScript type error:
import { ESLintUtils } from '@typescript-eslint/utils';
Cannot find module '@typescript-eslint/utils' or its corresponding type declarations.
This is not a bug. We intentionally dropped support in v6 for Node <16 as those versions of node are EOL. v6 uses package.json exports - which requires Node 16+ and isn’t supported by that older module resolution strategy.
To fix the issue, switch your TSConfig’s module resolution to a newer version, most likely "bundler", "node16", or "nodenext". See the aka.ms/tsconfig "moduleResolution" docs for more information.
{
"compilerOptions": {
"moduleResolution": "bundler"
}
}
See TypeScript’s new modules reference docs for more information.
Issues that are duplicates of this one:
- https://github.com/typescript-eslint/typescript-eslint/issues/7279
- https://github.com/typescript-eslint/typescript-eslint/issues/7213#issuecomment-1640401164
- https://github.com/typescript-eslint/typescript-eslint/issues/7185
https://arethetypeswrong.github.io/?p=%40typescript-eslint%2Futils%406.0.0 also shows a nice table of which moduleResolution values we support. It’s all of them except node10.
Keeping this issue open so it shows up more in searches.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 21
- Comments: 19 (9 by maintainers)
Commits related to this issue
- Update moduleResolution to nodenext This updates to a more modern moduleResolution, from the default of node/node10. Relevant typescript-eslint issue: https://github.com/typescript-eslint/typescript-... — committed to stianjensen/eslint-plugin-deprecation by stianjensen 3 years ago
- Update moduleResolution to nodenext This updates to a more modern moduleResolution, from the default of node/node10. Relevant typescript-eslint issue: https://github.com/typescript-eslint/typescript-... — committed to stianjensen/eslint-plugin-deprecation by stianjensen 3 years ago
- Upgrade `typescript-declarations` dependencies Although there is a later version of `@typescript-eslint/typescript-estree` available, using any later version will require updating to a newer module r... — committed to cph-cachet/carp.core-kotlin by Whathecode 3 years ago
- Upgrade `typescript-declarations` dependencies Although there is a later version of `@typescript-eslint/typescript-estree` available, using any later version will require updating to a newer module r... — committed to cph-cachet/carp.core-kotlin by Whathecode 3 years ago
- use `nodenext` for processing modules by TypeScript This is required to use `@typescript-eslint/utils` v6+. See: https://github.com/typescript-eslint/typescript-eslint/issues/7284 — committed to phanect/eslint-plugin-tree-shakable by phanect 3 years ago
- use `nodenext` for processing modules by TypeScript This is required to use `@typescript-eslint/utils` v6+. See: https://github.com/typescript-eslint/typescript-eslint/issues/7284 — committed to phanect/eslint-plugin-tree-shakable by phanect 3 years ago
- use `nodenext` for processing modules by TypeScript This is required to use `@typescript-eslint/utils` v6+. See: https://github.com/typescript-eslint/typescript-eslint/issues/7284 — committed to phanect/eslint-plugin-tree-shakable by phanect 3 years ago
- chore: upgrade `@typescript-eslint` Because `@typescript-eslint` uses package.json `exports`, we had to upgrade some dependencies which didn't support `exports` (see https://github.com/typescript-esl... — committed to lesha1201/eslint-plugin-testing-library by lesha1201 3 years ago
- Fix bug caused by @typescript-eslint update. see https://github.com/typescript-eslint/typescript-eslint/issues/7284#issue-1816154808 — committed to dorsale/dorsale by dj-256 3 years ago
- Fix bug caused by @typescript-eslint update. (#45) * Fix bug caused by @typescript-eslint update. see https://github.com/typescript-eslint/typescript-eslint/issues/7284#issue-1816154808 * CI: bum... — committed to dorsale/dorsale by dj-256 3 years ago
Sweet baby jesus, thank you so much! 🙏
Docs says it’s “very likely” that you want to have these configurations for modern node projects:
And this solves the issue aswell.
@GYuriy suggest just turning off that rule for TS files, tbh. It’s duplicating checks that TS already does for you and it’s logic requires separate configuration to even attempt to mirror the module resolution that TS uses. Also it does a lot of disk reads which makes it a pretty slow rule.
We don’t do it because it’s an inaccurate representation of our package. It allows users to misconfigure their project and then access private implementation details that don’t form part of our API.
nodeis (not very obviously) an alias fornode10which means that consumers using that setting are using module resolution logic which does not match the reality of what happens at runtime. Our minimum required node version is v16 - hencenode16is the correct setting to model the resolution logic used at runtime.Major version upgrades are the time for breaking changes and not all breaking changes are going to be easy for all codebases to update for. We do our best to minimise the impact but some changes are just hard.
As an example - in v9 eslint will remove some methods they have recently deprecated. For projects like us that support v7+ this will be really hard for us as we’ll need to create utils to help support both legacy and current versions. That’s on us and how we use eslint though - not on the eslint team to keep things around to make our lives easier.
Yeah, this seems like an issue on their end. I’d suggest filing an issue on
ts-nodelinking back to this discussion.We don’t use
ts-nodein typescript-eslint and instead have gone with https://swc.rs/docs/usage/jest. We haven’t wantedts-node-style type type checking in our tests (it slows down tests) and SWC is a faster transpiler than Babel or ts-node (https://www.joshuakgoldberg.com/blog/jest-babel-to-swc).I have some unit tests for custom rules that I used to run with
jest, but now it’s not possible.I’d gladly use
nodenext/nodenextorNode16/Node16, but you cannot override this with atsconfig.jsonfile in your project, becausets-nodeis registered insidejestwith a hardcoded"module": "CommonJS". See here:https://github.com/SimenB/jest/blob/08ef1058175ba0cf3ad6749442fe88ac45524845/packages/jest-config/src/readConfigFileAndSetRootDir.ts#L116-L123
So even if you change the
moduleandmoduleResolutionto, for example,Node16, this is what you get when runningjest:How should the
jestconfiguration now look like? Does it mean we now have to build the whole “custom rules” project as ESM?PS I know you’re not responsible for TS + jest + Node interplay, but I failed to find any information on what to do now. Maybe the answer is not that tricky and you happen to know it. It’s probably not only me having this issue, it was hard to find it on the web.
At this point it has been over 6 months since v6 has been released. The paths to fix the issue have been documented. We are not going to add back support for
node10.So I’m going to close this.
Figured out my issue. I had to change the absolute path of
parserwhen creating RuleTester after upgrading. e.g.to
@JoshuaKGoldberg, @JavaScriptBach replying to my own comment from half a year ago
Funnily enough, I totally forgot about this Jest + ts-node issue, and stumbled upon it again last month. So I reported a bug (https://github.com/jestjs/jest/issues/14740) (without mentioning this issue) and proposed a fix: https://github.com/jestjs/jest/pull/14739 (a quick mitigation of the bug is to use
jest.config.jsinstead ofjest.config.ts🙃 )@JavaScriptBach, trying to help you with swc, this is my
jest.config.js(maybe usage of “.js” matters here as well? IDK)I did not have to pull off any additional tricks
I’m just wondering: why don’t we add back the
"types"field in the package.json of the projects? It seems that it would fix issues and unlock the bump of some other open-source projects current relying on@typescript-eslint/*internally.For instance, I started to look at
eslint-plugin-jestand adding the"types"would just make the move to v6 smoother.The
"types"would not be read, when bundler or nodenext is set. But each of them may require huge revamps of the existing codebase to be able to toggle them.@AdamQuadmon without any details on your codebase or errors you’re seeing, I don’t know what you mean by “the issue”. Let’s go with one of two actionable followups:
Side note that I believe that specific code path is only used if you use a
jest.config.tsfile.Creating a custom rules package using
typescript-eslintand bumped intoCannot find module '@typescript-eslint/utils' or its corresponding type declarations.. The fix from DanielDanielson above worked for me.But I can’t see in the official documentation mentioned anywhere a need to change anything in
tsconfig.json. For the sake of better DX should the above page be enhanced with such information? Seems like a good spot as it’s literally “Building Custom Rules” page.