Igor T
Igor T

Reputation: 512

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

Upvotes: 29

Views: 14626

Answers (3)

JWP
JWP

Reputation: 6963

Was having similar issue with the GraphQL samples, where I had used create-react-app myapp --typescript.

TS1208

GraphQL compiler error

Was able to fix it like this:

enter image description here

Upvotes: 0

dburmeister
dburmeister

Reputation: 540

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.

Upvotes: 29

basarat
basarat

Reputation: 276195

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.

Upvotes: 26

Related Questions