Search code examples
typescriptgulpaureliagulp-watch

Error referencing my TypeScript modules TS2307: Cannot find module


I am working with the aurelia-typescript-skeleton as the base for my new project. I tried adding a new hello.ts file in src folder

export class Hello {
  sayHello(name:string) : string {
    return 'Hello ' + name;
  }
}

and referenced it in another file in the same folder as below

import {Hello} from './hello';

export class Users {
  constructor() {
    console.log(new Hello().sayHello('Test'));
  }
}

Both the files are at the same folder level. Everything works fine when I build for the first time. When I make any subsequent changes on the users.ts file, the gulp-typescript compilation keeps failing with an error I am unable to understand. The error from the typescript compiler is

> Starting 'build-system'...
> src\users.ts(4,21): error TS2307: Cannot find module 'hello'.
> TypeScript: 1 semantic error
> TypeScript: emit succeeded (with errors)
> Finished 'build-system' after 950 ms

Whenever i do a fresh gulp watch, there are no errors. The error appears when I edit/change the users.ts file. Can anyone help me understand this error? It must be something basic...

I'm on Windows 7 environment, and I get this error on 2 machines.

UPDATE:

Here is the repo to reproduce the problem. Steps to reproduce:

  1. Clone the repo, install all npm and jspm dependencies.
  2. Run gulp watch -> no errors occur for me
  3. Change users.ts file and save -> the error occurs.

UPDATE2:

Adding a clean step before build-system helps to avoid the problem. Here is the link to commit. Still I'm not sure about the actual reason of the problem at first hand.


Solution

  • It is because only users.ts, the changed file, is sent to subsequent TS compilation, while noResolve is enabled (in tsconfig.json).

    Please see file build/tasks/build.js, task 'build-system':

    .pipe(changed(paths.output, {extension: '.js'}))
    

    What files are changed are determined (by gulp-changed) by comparing last modified time of the input files with the destination. So when you run clean or watch, which involve clean, the destination is cleaned and all the files are sent to compilation again, thus no error.

    As I try the updated skeleton app (1.0.0-beta.1.2.2), this problem has been fixed.