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.
4
  • try var instead of import. var http = require('http') Commented Aug 22, 2013 at 10:58
  • 6
    gives me error TS2095: Could not find symbol 'require'.. All examples I've seen use the import declaration. Commented Aug 22, 2013 at 11:15
  • 1
    if you use var, your import won't be typed correctly. Commented Feb 24, 2016 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? Commented Mar 5, 2016 at 9:08

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

12 Comments

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!
I'm glad you didn't post code that included the reference. That would have become hard to track ;)
@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
@basarat I've asked a question about this here: stackoverflow.com/questions/20437808/…
All links to Github are broken. Is this answer still valid?
|
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.

Comments

-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 ?

4 Comments

module was deprecated in favor or require to be more consistent with the generated js
How would this work with a module already written in Typescript? Would it need a definition file? I've asked the question here also: stackoverflow.com/questions/23175152/…
@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.
no. module has been deprecated for a long time. Its import/require

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.