Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript compilation error TS5037: Cannot compile external modules unless the '--module' flag is provided

Cannot compile any TS+node.js project including listed in samples http://typescript.codeplex.com/sourcecontrol/latest#samples/imageboard/README.txt

Always get the following error:

error TS5037: Cannot compile external modules unless the '--module' flag is provided.

compiler's version: 0.9.1.0

For example, the project consists of just single file app.ts:


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

import http = require("http")

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, 'localhost');
console.log('Server running at http://localhost:1337/');

like image 605
Igor T Avatar asked Aug 13 '13 13:08

Igor T


2 Answers

Also just to add.

I am using Visual Studio 2013 I got this same error running build to fix it. I went to the properties of my project and then to the "TypeScript Build" section in there was the option to pick a module system I selected AMD it was at none.

like image 96
dburmeister Avatar answered Nov 01 '22 09:11

dburmeister


As mentioned compile with the module flag, e.g. if your file is called myfile.ts :

tsc myfile.ts --module "commonjs" 

The reason is that starting from TSC 0.9.1 the default module option is amd (e.g. requirejs) which is the most common module pattern for client side javascript code. So, you need to specify the module option to get commonjs code which is the most common module pattern for server side javascript code (e.g. nodejs) which is why the compiler is prompting you to be explicit about your target :) This prompt occurs when you do an import on an external module.

like image 25
basarat Avatar answered Nov 01 '22 10:11

basarat