angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Module not found when using type from .d.ts file

Open Buslowicz opened this issue 9 years ago • 40 comments

I started this issue on angular/angular issues, but seems to be related to CLI, so just copy-pasting the issue here :).

I'm submitting a ...

[x] bug report

Current behavior When assigning a custom type (like Post, or User) to a decorated property (with either ng decorator like @Input() or a custom one), where type is declared in d.ts file, angular throws an error:

ERROR in ./src/app/app.component.ts
Module not found: Error: Can't resolve './types' in '/home/dracco/projects/ng2/src/app'
 @ ./src/app/app.component.ts 11:0-31
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts
webpack: Failed to compile.

This does not happen if:

  • type is added through generic (like Array<Post>) OR
  • no decorator is added to property OR
  • file with declaration has .ts extension instead of .d.ts

Expected behavior No error should be thrown. property should have given type.

Minimal reproduction of the problem with instructions

  1. Run ng init to create a new Angular project.
  2. Create a d.ts file in app folder
  3. Add an exported interface to the .d.ts file
  4. Import above interface in component file
  5. Add a property with decorator and above type (e.g. @Input() prop: MyType)
  6. Run ng serve

What is the motivation / use case for changing the behavior? The default workflow in TypeScript suggests keeping declarations and interfaces in .d.ts file. Simple Angular components might not use Observables and still accept data from the host. I am actually surprised nobody mentioned and solved it so far as it is pretty severe (it also happens in 4.0.0-beta.8).

Please tell us about your environment:

  • OS: linux x64 (Ubuntu 16.10)
  • Angular version: 2.3.1, 2.2.4, 4.0.0-beta.8
  • Angular CLI version: 1.0.0-beta.28.3
  • Node (for AoT issues): 7.5.0
  • NPM: 4.1.2

Buslowicz avatar Feb 21 '17 14:02 Buslowicz

@filipesilva and @hansl can you estimate when there will be time to fix this issue? I see it has been labeled 'priotity urgent' but so far nothing has happened.

basst314 avatar Aug 08 '17 11:08 basst314

Just ran into this issue today, is there a fix planned in the near future? I'm pulling in a third-party set of components that is triggering this issue.

SamD avatar Aug 22 '17 22:08 SamD

Not using angular-cli, and having the exact same issue, I actually don't think it's angular-cli related.

RDeluxe avatar Aug 29 '17 20:08 RDeluxe

Same here. Not using cli and get the same error. You can see that index.d.ts is never looked for if you follow the whole thing through.

ERROR in ./src/app/menu/menu.component.ts
Module not found: Error: Can't resolve './shared' in 'D:\Development\client\src\app\menu'
resolve './shared' in 'D:\Development\client\src\app\menu'
  using description file: D:\Development\client\package.json (relative path: ./src/app/menu)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: D:\Development\client\package.json (relative path: ./src/app/menu)
    using description file: D:\Development\client\package.json (relative path: ./src/app/menu/shared)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\client\src\app\menu\shared is not a file
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\client\src\app\menu\shared.ts doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        D:\Development\client\src\app\menu\shared.js doesn't exist
      as directory
        existing directory
          using path: D:\Development\client\src\app\menu\shared\index
            using description file: D:\Development\client\package.json (relative path: ./src/app/menu/shared/index)
              no extension
                Field 'browser' doesn't contain a valid alias configuration
                D:\Development\client\src\app\menu\shared\index doesn't exist
              .ts
                Field 'browser' doesn't contain a valid alias configuration
                D:\Development\client\src\app\menu\shared\index.ts doesn't exist
              .js
                Field 'browser' doesn't contain a valid alias configuration
                D:\Development\client\src\app\menu\shared\index.js doesn't exist
[D:\Development\client\src\app\menu\shared]
[D:\Development\client\src\app\menu\shared.ts]
[D:\Development\client\src\app\menu\shared.js]
[D:\Development\client\src\app\menu\shared\index]
[D:\Development\client\src\app\menu\shared\index.ts]
[D:\Development\client\src\app\menu\shared\index.js]

Asken avatar Sep 13 '17 09:09 Asken

I can confirm

FrancescoBorzi avatar Oct 11 '17 10:10 FrancescoBorzi

Another workaround similar to marshall007's is to declare it like this:

export class FooComponent { @Input() public foo: IFoo; // does not work @Input() public foo: IFoo | any; // works }

PhotoPaul avatar Oct 18 '17 10:10 PhotoPaul

I can also confirm what @PhotoPaul said.

This is a serious issue, I hope it can be solved as soon as possible.

FrancescoBorzi avatar Nov 04 '17 14:11 FrancescoBorzi

@hansl let's get some kind of resolution please?

vmorris avatar Nov 17 '17 19:11 vmorris

In my case it seems to be failing when it is loading the typescript via webpack (see ng eject -ed webpack config):

      {
        "test": /\.ts$/,
        "loader": "@ngtools/webpack"
      }

I get this myself when I use webpack (the 'Fingerprint2' class name corresponds to a .d.ts file I have under node_modules and typescript's compiler finds it fine):

error TS2304: Cannot find name 'Fingerprint2'. error TS2304: Cannot find name 'Fingerprint2'. error TS7006: Parameter 'result' implicitly has an 'any' type. error TS7006: Parameter 'components' implicitly has an 'any' type.

it seems like it isn't finding the .d.ts file in the @ngtools/webpack loader when running webpack.

curtstate avatar Jan 29 '18 17:01 curtstate

In my project I'm getting the same error message as Asken. It only happens when using AOT. And I also narrowed it down to using a type imported from a package (with .d.ts) inside a decorator (e.g. @NgModule). If I use the imported type outside of a decorator, the build succeeds. In fact, I'm probably going to use InjectionTokens as a workaround, because then I won't have to use the imported types inside decorators; and using them for injection in component constructor seems to be fine. Also if I just define the interface in the same file (not .d.ts), then it works fine inside a decorator. I tried this in both Angular 5 and 6.

The error message itself seems to originate from inside enhanced-resolver. The file it can't resolve is inside the package only as a .d.ts file.

ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve 'my-interface-package/lib/IMyService' in '<project folder>\src\app'
My setup:
import { IMyService } from 'my-interface-package';
import { MyActualService } from 'my-implementation-package';

// IMyService is actually not an interface, but an abstract class, so it can be used for DI.

@NgModule({
    providers: [
        { provide: IMyService, useClass: MyActualService }
    ]
})
export class AppModule {}

Planned workaround for now:

export const MY_SERVICE_TOKEN = new InjectionToken('IMyService');
@NgModule({
    providers: [
        { provide: MY_SERVICE_TOKEN, useClass: MyActualService }
    ]
})
export class AppModule {}

@Component({
    template: '...',
})
export class SomeComponent {
    // Using IMyService here for parameter type does not cause an error.
    constructor(@Inject(MY_SERVICE_TOKEN) private myService: IMyService) {}
}

Pumpuli avatar Aug 09 '18 11:08 Pumpuli

I'm still have this issue as well. In AOT only

Jerome2606 avatar Aug 24 '18 15:08 Jerome2606

I started getting this today, where yesterday I didn't have it. The only thing I can think of is that between now and then, I upgraded to the following:

    "@angular-devkit/build-angular": "~0.8.0-rc.0",
    "@angular-devkit/build-ng-packagr": "~0.8.0-rc.0",
    "ng-packagr": "^4.1.0",

from:

    "@angular-devkit/build-angular": "~0.6.8",
    "@angular-devkit/build-ng-packagr": "~0.6.8",
    "ng-packagr": "^3.0.0-rc.2",

I am going to revert, rebuild, and report back.

dudewad avatar Aug 28 '18 16:08 dudewad

Reverting did not help. I'm still going to keep poking around. FWIW, I'm building libraries (separate projects created by ng generate library) that compile to dist in my root directory. I for some reason feel like they WERE exporting index.ts files and are now only building to index.d.ts files, but I could definitely be wrong about that. All I know is, index.d.ts files, as far as I know, are not compiled into the project bundles. Am I correct in thinking that?

dudewad avatar Aug 28 '18 16:08 dudewad

I'm not sure if I'm even experiencing the same issue. However, I pointed my tsconfig aliases for my library projects from their dist/projectname directories (the built projects) to the source projects (libs/projectname/src) and it compiled fine. I'm concerned that that will de-optimize my build, though... is that something I should worry about, or is this okay to do?

dudewad avatar Aug 28 '18 16:08 dudewad

@dudewad Can you please provide a reproduction repository we can use to investigate this? It seems there are some metadata emitted from Angular's decorators, and we should be stripping those before we compile in AOT.

hansl avatar Sep 13 '18 17:09 hansl

@dudewad This seems like a different issue. If you could provide a reproduction and create a new issue (here), that'd be great. Thanks!

I'm closing this issue as fixed.

hansl avatar Sep 13 '18 17:09 hansl

This bug is still happening with version 7.1.4 and it's quite annoying.

Also (hoping that this can help with the bug resolution), I noticed that if you create two files like:

  • myfile.t
  • myfile.d.ts

and try to include in myfile.ts something exported by myfile.d.ts, the compiler will say that there is a circular dependency -> so it doesn't distinguish between those two files.

FrancescoBorzi avatar Dec 28 '18 13:12 FrancescoBorzi

Yes, This bug is still happening with version 7.1.4.

LokeshBadgujar avatar Jan 05 '19 06:01 LokeshBadgujar

I had the same problem: Files with the ending .d.ts could not be resolved.

In my case the problem caused by the option annotateForClosureCompiler of the angularCompilerOptions: https://angular.io/guide/aot-compiler#annotateforclosurecompiler

Setting annotateForClosureCompiler to false (or removing it, since false is the default value) in tsconfig.json fixed the problem:

"angularCompilerOptions": {
    "annotateForClosureCompiler": false
}

cbossi avatar Mar 21 '19 18:03 cbossi

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.