TypeScript getting error TS2304: cannot find name ' require'

后端 未结 23 3183
Happy的楠姐
Happy的楠姐 2020-11-22 06:00

I am trying to get my first TypeScript and DefinitelyTyped Node.js application up and running, and running into some errors.

I am getting the error \"TS2304: Cannot

23条回答
  •  被撕碎了的回忆
    2020-11-22 06:39

    For TypeScript 2.x, there are now two steps:

    1. Install a package that defines require. For example:

      npm install @types/node --save-dev
      
    2. Tell TypeScript to include it globally in tsconfig.json:

      {
          "compilerOptions": {
              "types": ["node"]
          }
      }
      

    The second step is only important if you need access to globally available functions such as require. For most packages, you should just use the import package from 'package' pattern. There's no need to include every package in the tsconfig.json types array above.

提交回复
热议问题