typescript

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/');


Solution

  • 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.