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://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

Most upvoted comments

Sweet baby jesus, thank you so much! 🙏

Docs says it’s “very likely” that you want to have these configurations for modern node projects:

"compilerOptions": {
    "moduleResolution": "nodenext",
    "module": "nodenext",
    //...
  }

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.

node is (not very obviously) an alias for node10 which 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 - hence node16 is 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.

because ts-node is registered inside jest with a hardcoded "module": "CommonJS".

Yeah, this seems like an issue on their end. I’d suggest filing an issue on ts-node linking back to this discussion.

We don’t use ts-node in typescript-eslint and instead have gone with https://swc.rs/docs/usage/jest. We haven’t wanted ts-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/nodenext or Node16/Node16, but you cannot override this with a tsconfig.json file in your project, because ts-node is registered inside jest with 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 module and moduleResolution to, for example, Node16, this is what you get when running jest:

TS5110: Option 'module' must be set to 'Node16' when option 'moduleResolution' is set to 'Node16'.

How should the jest configuration 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 parser when creating RuleTester after upgrading. e.g.

import { TSESLint } from "@typescript-eslint/utils";

const ruleTester = new TSESLint.RuleTester({
  parser: resolve(
    __dirname + "../../../../../node_modules/@typescript-eslint/parser",
  ),
});

to

const ruleTester = new TSESLint.RuleTester({
  parser: resolve(
    __dirname + "../../../../../node_modules/@typescript-eslint/parser/dist",
  ),
});

@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.js instead of jest.config.ts 🙃 )


@JavaScriptBach, trying to help you with swc, this is my jest.config.js (maybe usage of “.js” matters here as well? IDK)

module.exports = {
  testMatch: ['**/*.spec.ts'],
  transform: {
    '^.+\\.(t|j)s$': '@swc/jest',
  },
  coverageDirectory: '<rootDir>/../../../test-reports/eslint-plugin-unit-tests',
};

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-jest and 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:

  • If what you’re seeing is exactly #7273, then yes, see if that fixes your issue
  • If what you’re seeing is not resolved by #7273 and you believe is something specifically relevant to this specific issue, then please provide a reproduction that we can use to help you

Side note that I believe that specific code path is only used if you use a jest.config.ts file.

Creating a custom rules package using typescript-eslint and bumped into Cannot 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.