16

I'm having an issue in VSCode using the out-of-the-box create-react-app my-app --template typescript project not recognizing any element. I constantly get the error cannot find name xxx with 'xxx' being whatever the HTML element I'm using in the JSX.

What's interesting is that the project will run initially with zero edits. As soon as I actually go into App.tsx and change anything or create a very basic new component it breaks.

Here's a very basic component I attempted to make following this tutorial from the TypeScript Handbook Github.

// src/components/Hello.tsx
import React from 'react';

export const Hello = () => {
  return <h1>Hello < /h1>;
};

Once more this project is created directly from the recommended TypeScript template using create-react-app my-app --template typescript. No files were edited.

It came with their own tsconfig.json ready to go.

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Of course they had their own package.json as well.

{
  "name": "ts-trial",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.52",
    "@types/react-dom": "^16.9.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.3",
    "typescript": "4.0.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Once more no files were edited - I ran the command, cded into my new project, created Hello.tsx and as soon as I put an element in the return statement it said Cannot find name 'h1'. ts(2304).

Then I went to App.tsx and as soon as I opened it the same error was showing all over too.

I filed an issue on GitHub as I don't see how I could be getting this error honestly. I've searched for hours and most of the solutions I found weren't relevant for my particular issue. Most solutions were forgetting to change file extensions from .js or .ts to .tsx for React. Or they had to specify "@types/node": "^12.0.0" in package.json, or target, lib, module or include in tsconfig.json, all of which I have.

Environment Info:

System:

  • OS: macOS 10.15.6
  • CPU: (4) x64 Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz

Binaries:

  • Node: 14.6.0 - /usr/local/bin/node
  • Yarn: 1.22.4 - /usr/local/bin/yarn
  • npm: 6.14.7 - /usr/local/bin/npm

Browser:

  • Firefox Developer Edition 82.0b7 (64-bit)

npmPackages:

  • react: ^16.13.1 => 16.13.1
  • react-dom: ^16.13.1 => 16.13.1
  • react-scripts: 3.4.3 => 3.4.3

npmGlobalPackages:

  • create-react-app: 3.4.1

Code Editor: VSCode

1
  • Could you make a Github repo with the whole project (except node_modules, to save space) committed to it? It would be a lot easier to help debug this as a folder, where we could see your organization, rather than copy-pasted in here. Commented Jan 18, 2021 at 18:41

6 Answers 6

43

The file extension has to be .tsx in order for this to work.

Sign up to request clarification or add additional context in comments.

Comments

6

I have renamed my file .tsx, but the problem persisted. it was with how i exported my component. I tried this and it worked.

My component was

export default InsertDocuments = () =>

I changed it to

const InsertDocuments = () =>
//etc
export default InsertDocuments

or

export const InsertDocuments = () =>

Comments

4

Setting the VS Code typescript version to the version inside your workspace should fix it.

See: https://stackoverflow.com/a/64976666/6753500

Comments

4

I run into the same problem. I discovered that I had 100 extensions installed in VS Code and one of them was causing the problem. I dont know which one it was but, after I disabled all the extensions, it started working.

2 Comments

An extension was also the issue for me, I tried installing a lot of snippet ones and others I wasn't using. Don't know exactly which one it was.
VS-code extension "React-Native Custom Snippets" was bringing the issue. after disabling it and restarting the VS-code, it worked for me.
2

In my case, it was HCL AppScan Security extension which was causing trouble. Disabled it and then it worked like charm.

Comments

-1

Extra space in close tag < /h1> -> </h1>

2 Comments

The extra space is a result of Prettier trying to format with an error present. Unfortunately, if I correct the spacing and save it will go straight back to that due to the underlying error of cannot find name xxx. The App.tsx file is even worse after saving due to the same errors.
This seems to have received a bunch of downvotes, but I was able to fix the problem with the reported error in VS Code by adjusting spacing in my code base, so it's plausible to me that this was a genuine solution to the issue, if unexplained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.