Kewin Dousse
Kewin Dousse

Reputation: 4027

Typescript : Can't find module at execution

I've been struggling with the different ways to make modules in Typescript, trying to encapsulate my code elegantly. However, I think I'm lost with how some things are supposed to work.

Essentially, I'm trying to make two projects work together :

The type definitions seem to be ok. The problems come with the library. So I'm trying to keep the definitions in a module named riotGamesApi, and an other module named riotGamesTypeNode for the library. I tried importing the first in the second, and the pre-compiler doesn't detect anything wrong.

I also started writing tests using mocha for my library. Again, the pre-compiler doesn't find anything wrong in my code.

Here is what I'm currently doing with the modules :

File riotgamesapi.d.ts (type definitions)

declare module riotGamesApi {
    export module champion {
        [...]
    }
    [some more exports]
}

declare module "riotGamesApi" {
    export = riotGamesApi;
}

File riotgamesapi-typenode.ts (library)

///<reference path="../lib/riotgamesapi-typedef/riotgamesapi" />
///<reference path="../typings/node/node" />

import * as api from "riotGamesApi";

export module riotGamesTypeNode {
    [some classes here]
}

export = riotGamesTypeNode;

File riotgamesapi-typenode-tests.ts (library tests)

///<reference path="../lib/riotgamesapi-typedef/riotgamesapi" />
///<reference path="../src/riotgamesapi-typenode" />

import * as api from 'riotGamesApi';
import * as rtnode from '../src/riotgamesapi-typenode';

[Using rtnode in code]

But after compiling everything (with gulp using commonjs and es5), when I try to run the tests, this error is encountered :

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: Cannot find module '../src/riotgamesapi-typenode'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:289:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    [...more error lines...]

I don't understand what I'm doing wrong here, as this seems to be the only way the pre-compiler doesn't warn me about something. I'm surely missing some things, but then so is the pre-compiler ?

I've linked to the contents of the complete files so you can look for more info if you need, or try it yourself by cloning the git. (I'm using gulp for this, the task to test the code is gulp test)

Thank you in advance for any piece of advice or solution you might have, because I've been stuck with this for some time now... :)

Upvotes: 0

Views: 757

Answers (1)

Vadim Macagon
Vadim Macagon

Reputation: 14847

Look at your build output directory, there's no src dir in there so ../src/riotgamesapi-typenode in your tests will never be resolved to riotgamesapi-typenode.js. Fix your Gulp config to output the expected directory structure.

I should also mention that mixing namespaces (AKA internal modules) with external modules as you do in riotgamesapi-typenode.ts is generally a bad idea, and using reference path comments is no longer a recommended practice (reference them in your Gulp task instead).

Upvotes: 1

Related Questions