Skip to content

Cannot find name 'X'

Error message

                Cannot find name
              

Quick summary

You referenced a name (variable, function, or type) that TypeScript cannot find in the current scope or imports.

Why this happens

  • Typo in the variable or function name

  • Forgot to import the name from another module

  • The variable is declared in a different scope

  • Missing type declaration for a global variable

Minimal example

✗ Broken code
console.log(userNmae); // typo: not declared
✓ Fixed code
const userName = "Alice";
console.log(userName);

Declare the variable before using it, and fix any typos.

How to diagnose

  • Check the spelling of the name

  • Verify the import statement includes the name

  • Check that the name is declared in an accessible scope

  • If it's a global, add a declaration file (.d.ts)

How to fix

  • Fix the typo to match the declared name

  • Add the missing import statement

  • Move the declaration to a scope accessible from the usage

  • Add a global declaration in a .d.ts file

How to prevent

  • Use an IDE with autocomplete to avoid typos

  • Enable ESLint with no-undef rule

  • Organize imports automatically

Related Resources

Related Glossary

← Back to language