Importing node-modules with TypeScript

后端 未结 3 1432
南方客
南方客 2020-11-29 11:10

I\'m trying to get this to work, but I can\'t seem to find a solution anywhere on SO. When trying to compile this single-file app:

import http = require(\'ht         


        
相关标签:
3条回答
  • 2020-11-29 11:36

    TypeScript needs to know that http is present.

    Updated

    Install the type definitinos for node:

    npm install @types/node
    

    Old answer

    Follows these two steps

    • Download the node.d.ts file from here : https://github.com/borisyankov/DefinitelyTyped/tree/master/node
    • At the top of your file add:

      /// <reference path="node.d.ts" />
      

    PS: See a sample test file : https://github.com/borisyankov/DefinitelyTyped/blob/master/node/node-tests.ts

    0 讨论(0)
  • 2020-11-29 11:39

    Shouldn't it be something like

    /// <reference path="node.d.ts" />
    import http = module('http')
    

    I mean, shouldn't you use module instead of require ?

    0 讨论(0)
  • 2020-11-29 11:41

    I found that I had noResolve set to true in my tsconfig.json file. This was causing errors with the references to the .d.ts files that I had included at the top of my TypeScript files.

    0 讨论(0)
提交回复
热议问题