Home·Error messages

tsc · Build and types

The message

error TS2307: Cannot find module 'lodash' or its corresponding type declarations.

What it means

tsc found neither the module itself nor any type declarations for it, and it matters that the message says both — the package may be missing entirely, or installed but shipping no types. If it is installed, the types are what is missing: npm i -D @types/lodash adds the companion package, though many modern packages bundle their own types and have no @types at all. When this fires on a relative import instead, look at letter case and at the paths in tsconfig — a path into your own files has nothing to do with @types.

The fix

npm i -D @types/lodash
Printed by
tsc
Build and types
10

Build errors are a tool refusing before anything runs, so they break nothing at the moment they appear — but they do ask two questions: will you fix the type or the path to match the real shape, or will you push that one line through with an as any or a suppression comment — and it is here that the things which are only true on your own machine, letter case, extensions and environment variables, show themselves for the first time.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “error TS2307: Cannot find module 'lodash' or its corresponding type declarations.” mean?

tsc found neither the module itself nor any type declarations for it, and it matters that the message says both — the package may be missing entirely, or installed but shipping no types. If it is installed, the types are what is missing: npm i -D @types/lodash adds the companion package, though many modern packages bundle their own types and have no @types at all. When this fires on a relative import instead, look at letter case and at the paths in tsconfig — a path into your own files has nothing to do with @types.

Q. How do I fix it?

npm i -D @types/lodash — before running it, check the explanation above for what this command discards.

Q. Which tool prints this?

tsc. It sits under Build and types, and the message runs to 11 words.

Errors nearby