TypeScript getting error TS2304: cannot find name ' require'

后端 未结 23 2540
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

    Electron + Angular 2/4 addition:

    On top of adding the 'node' type to ts.config various files, what eventually worked for me was adding the next to the typings.d.ts file:

    declare var window: Window;
    interface Window {
      process: any;
      require: any;
    }
    

    Note that my case is developing with Electron + Angular 2/4. I needed the require on the window global.

    0 讨论(0)
  • 2020-11-22 06:39

    Just in addition to cgatian's answer, for TypeScript 1.x

    If you are still seeing errors, please specifically provide index.d.ts in your compiler options.

    "files": [
        "typings/index.d.ts"
    ]
    
    0 讨论(0)
  • 2020-11-22 06:40

    If you can compile code, but Visual Studio 2015 marks 'require' functions as errors with error text cannot find name 'require' or cannot resolve symbol 'require', update TypeScript for Visual Studio 2015 to the latest version (at the moment 2.1.5) and update ReSharper (if you use it) to at least version 2016.3.2.

    I had this annoying problem for a while and couldn't find a solution, but I finally resolved it this way.

    0 讨论(0)
  • 2020-11-22 06:43
    import * as mongoose from 'mongoose'
    
    0 讨论(0)
  • 2020-11-22 06:43

    If you are facing this issue in a .ts file which is only there to provide you some constant values, then you can just

    rename your .ts file to .js file

    and the error will not come again.

    0 讨论(0)
  • 2020-11-22 06:45

    For me it is resolved by adding types to the angular compiler options.

    "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "types": [ "node" ]
    }
    
    0 讨论(0)
提交回复
热议问题