TypeScript getting error TS2304: cannot find name ' require'

后端 未结 23 3088
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:27

    This answer relates to modern setups (TypeScript 2.x, Webpack > 2.x)

    You don't need to install @types/node (which is all of Node.js types and is irrelevant for front-end code, actually complicating things such as setTimout different return values, etc..

    You do need to install @types/webpack-env

    npm i -D @types/webpack-env

    which gives the runtime signatures that Webpack has (including require, require.ensure, etc.)

    Also make sure that your tsconfig.json file has no set 'types' array -> which will make it pickup all type definitions in your node_modules/@types folder.

    If you want to restrict search of types you can set the typeRoot property to node_modules/@types.

提交回复
热议问题