37

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('http')
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

Using the command "tsc app.ts --module 'commonjs'" I get the following error (not using the --module flag gives me an additional error telling me that I need it to compile external modules):

error TS2071: Unable to resolve external module '"http"'.
error TS2072: Module cannot be aliased to a non-module type.
jorgenbs
  • 557
  • 1
  • 4
  • 9
  • try `var` instead of `import`. `var http = require('http')` – nikodz Aug 22 '13 at 10:58
  • 6
    gives me `error TS2095: Could not find symbol 'require'.`. All examples I've seen use the `import` declaration. – jorgenbs Aug 22 '13 at 11:15
  • 1
    if you use var, your import won't be typed correctly. – Shoerob Feb 24 '16 at 16:31
  • 1
    @Shoerob Can you elaborate a bit more on your comment. Do you mean that if we use 'var' instead of 'import', intelliSense wouldn't work on it? –  Mar 05 '16 at 09:08

3 Answers3

45

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

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

basarat
  • 261,912
  • 58
  • 460
  • 511
  • 1
    I'm scratching my head now, since I was sure I had tried adding the referencepath. I even had it in my folder. Anyway, it works now, thanks! – jorgenbs Aug 22 '13 at 12:45
  • I'm glad you didn't post code that included the reference. That would have become hard to track ;) – basarat Aug 22 '13 at 12:46
  • 1
    @basarat I copies the node.d.ts file you've pointed to, but its throwing plenty of "File not found errors" in the .d.ts file - especially for files like "crypto" "net" and "stream". Can you point me to these files or a stable version of node.d.ts – EternallyCurious Dec 07 '13 at 03:55
  • @basarat I've asked a question about this here: http://stackoverflow.com/questions/20437808/how-can-you-enable-nodejs-and-expressjs-into-typescript – EternallyCurious Dec 07 '13 at 05:16
  • How would this work with a module already written in Typescript? Would it need a definition file? I've asked the question here also: http://stackoverflow.com/questions/23175152/do-modules-written-in-typescript-need-a-definition-file-for-import – conradkleinespel Apr 19 '14 at 21:30
  • Worked for me too, though it's unclear why I need to add some external files as hacks (node.d.ts) in order for TypeScript to work... – Centurion Jan 03 '15 at 16:17
  • @centurion you cannot have type safety without telling the compiler about the types. – basarat Jan 03 '15 at 18:01
  • `wget https://raw.githubusercontent.com/borisyankov/DefinitelyTyped/master/node/node.d.ts` Save yourself a second on github :) – jocull Feb 03 '15 at 00:34
  • @jocull why stop there. `tsd install node` ;) – basarat Feb 03 '15 at 00:42
  • Could you explain what node.d.ts does in your answer? – arthur.sw May 17 '16 at 08:57
  • 4
    All links to Github are broken. Is this answer still valid? – Naktibalda Aug 03 '17 at 11:34
  • @Naktibalda, take a look at the question [How to find module “fs” in MS Code with TypeScript?](https://stackoverflow.com/questions/37260901/how-to-find-module-fs-in-ms-code-with-typescript) It's an more recent solution – rchavarria Sep 11 '17 at 14:28
2

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.

harvzor
  • 2,832
  • 1
  • 22
  • 40
-3

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 ?

Alessandro L.
  • 398
  • 1
  • 15
  • 9
    `module` was deprecated in favor or `require` to be more consistent with the generated js – basarat Jan 24 '14 at 05:58
  • How would this work with a module already written in Typescript? Would it need a definition file? I've asked the question here also: http://stackoverflow.com/questions/23175152/do-modules-written-in-typescript-need-a-definition-file-for-import – conradkleinespel Apr 19 '14 at 21:30
  • @basarat aren't 'module' and 'require' separate in typescript's context. I was of the opinion that 'module' is for internal modules and 'require' is for external modules. –  Mar 05 '16 at 09:10
  • no. `module` has been deprecated for a long time. Its `import/require` – basarat Mar 06 '16 at 23:53