1
votes

I am trying to port a javascript app to typescript that uses requirejs. I am using the typedefinition from @mhegazy at DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped/tree/master/crossroads Typescript compiler is giving me the following error...

error TS2307: Cannot find external module 'crossroads'.

This did all work in javascript using "defines" instead.

I just used this library as a complete example. What am I missing? Documentation is hard to dig up if TSC is really even paying attention to a requirejs config. I know crossroads depends on js-signals. Is there a problem with definitions and ts files being in different directories? From what I have read here I should not have to even put the reference comment. But if I then try to use a relative path, it cannot find it either.

Here is my app structure

  • index.html

Application ts/js

  • app/config.ts
  • app/bootup.ts
  • app/someclass.ts

Typescript definitions

  • typings/crossroads/crossroads.d.ts
  • typings/requirejs/require.d.ts

Javascript libraries

  • bower_components/crossroads/dist/crossroads.js
  • bower_components/requirejs/require.js

index.html has this script include

<script data-main="app/config" src="bower_components/requirejs/require.js>
</script>

/app/config.ts:

/// <reference path="../typings/requirejs/require.d.ts" />
(function () {
    requirejs.config({
        baseUrl: ".", 
        paths: { 
            "crossroads": "bower_components/crossroads/dist/crossroads"
        }
    })
    require(["app/bootup"]);
}) ();

/app/someclass.ts:

/// <reference path="../typings/crossroads/crossroads.d.ts" />
import crossroads = require("crossroads");

class SomeClass{
 // do something with crossroads
}
1

1 Answers

1
votes

import crossroads = require("crossroads");

To use import/require you either need:

  • relative path pointing to a TypeScript file.
  • a declared external module.

In your case you need to declare an external module for TypeScript to know about it. E.g.:

declare module 'crossroads'{
    export = crossroads;
} 

UPDATE I've updated the definitions so you don't need to do this : https://github.com/borisyankov/DefinitelyTyped/pull/3866 Enjoy!