12

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

Nick
  • 151
  • 1
  • 1
  • 6
  • 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. – Zack Jan 18 '21 at 18:41

6 Answers6

32

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

Jakub Klimek
  • 433
  • 6
  • 18
5

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 = () =>
shiraz27
  • 1,898
  • 18
  • 14
3

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

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

jokorone
  • 86
  • 1
  • 6
2

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 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. – Andres Zapata Apr 03 '22 at 09:28
0

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

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 09 '22 at 10:38
-3

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

Gleb Shaltaev
  • 234
  • 1
  • 6
  • 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. – Nick Oct 13 '20 at 14:45