Reputation: 557
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.
Upvotes: 37
Views: 49773
Reputation: 276235
TypeScript needs to know that http
is present.
Install the type definitinos for node:
npm install @types/node
Follows these two steps
node.d.ts
file from here : https://github.com/borisyankov/DefinitelyTyped/tree/master/nodeAt 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
Upvotes: 45
Reputation: 2908
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.
Upvotes: 2
Reputation: 405
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
?
Upvotes: -3