Typescript: tsc shows error 'Cannot find external module', but it works !?!

Created on 10 Jul 2015  路  2Comments  路  Source: microsoft/TypeScript

Hi team,

I get an error in the typescript compiler which I cant seem to understand how to fix. The error is:
>ex.ts(3,27): error TS2307: Cannot find external module 'autobahn'.

I'm transpiling like:
>tsc -m commonjs -t es5 "./typings/autobahn/autobahn.d.ts" ex.ts
and i also tried
>tsc -m commonjs -t es5 ex.ts
..but funnily enough, it DOES produce the correct ES5 javascript source, and it runs and works fine ?!?

...and my code is simply:

/// <reference path="./typings/autobahn/autobahn.d.ts" />
import autobahn = require('autobahn');
class C {
    x: number;
}
var myC = new C();`

..and I execute with
>node ex

It's like tsc can't find the module/file 'autobahn' ???

On my Windows 7 box, I have the latest release of node and npm. I have used npm to install autobahnJS (just any standard javascript library https://github.com/tavendo/AutobahnJS), Typescript v1.5.0-beta (https://github.com/Microsoft/TypeScript) and tsd (the typescript definition manager https://github.com/DefinitelyTyped/tsd).

I have used tsd to install the autobahn definitely types (it put it into the default folder ./typings/autobahn) and I reference it in my source code as you can see above.

All seems to be ok. As i mentioned, it seems to transpile just fine, the ES5 javascript output is as expected, and it runs.

It's a bit annoying having these error messages - when there is no error? Whats going on?

Cheers;

By Design

Most helpful comment

This is actually quite subtle, the TS2307 error means it tried to look for a file called autobahn.ts and did not find it. It does this because autobahn.d.ts failed to declare the module as an "Ambient External Module".

You can write an additional d.ts file to import the incorrect d.ts and re-export the module. Please also consider submitting a patch to DefinitelyTyped.

All 2 comments

This is actually quite subtle, the TS2307 error means it tried to look for a file called autobahn.ts and did not find it. It does this because autobahn.d.ts failed to declare the module as an "Ambient External Module".

You can write an additional d.ts file to import the incorrect d.ts and re-export the module. Please also consider submitting a patch to DefinitelyTyped.

As usual, @billccn's explanation is correct.

TypeScript will always try to emit code, even if it thinks it's not going to work. In your case the code was fine but the definition file was incomplete, so it turned out to be useful behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  路  3Comments

dlaberge picture dlaberge  路  3Comments

remojansen picture remojansen  路  3Comments

bgrieder picture bgrieder  路  3Comments

jbondc picture jbondc  路  3Comments