cannot determine the module for component angular 5

后端 未结 12 2015
太阳男子
太阳男子 2020-12-29 20:39

I\'m getting following error when I build the Angular app using \"ng build --prod\". This is working when I build with Angular 4 and getting error with Angular 5. With Angul

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-29 21:13

    Ran into this issue and wanted to consolidate existing suggestions, and suggest some general strategies when nothing else worked for me:

    1. In my case, the error was it could not determine the module for a Pipe; I was using custom Pipe injected straight into an Angular Service.
    • When I stopped using the Pipe, the issue went away; so I removed the Pipe transform -- I moved the transformation to a different Service, stopped depending on Pipe altogether.
    1. Make sure you declared (and exported), your Pipe/Component properly in the declarations (and exports) properties of your NgModule
      • Source: Angular best practice, NgModule basics
    2. If it is a 3rd party Pipe/Component (i.e. inside node_modules), copy the Pipe to your own project (if possible), and/or re-export the Pipe from one of your NgModule
      • Source: the accepted answer here
    3. If you don't need the Pipe/Component, you can exclude the file by adding to the exclude property of the tsconfig.json (this may not be an option for everyone)
      • Source: also accepted answer
    4. Or build without ahead-of-time compilation ng build --aot=false (may be a faster build, but then creates a non-optimized app; learn more about AOT)
      • Source: Again the accepted answer
    5. If it is your Pipe/Component (vs 3rd party), confirm you don't double-define your Pipe class, and don't double-declare your Pipe: i.e. add to the declaratations array of two or more different NgModules
      • Source: Another answer here
    6. In your Angular typescript files where you import { YourPipe } from './yoUr-pippe.ts', make sure there are no typos or capitalization issues in your import statement, especially the filename
      • (my example here './yoUr-pippe.ts' has a capitalization issue, and a typo, and shouldn't use the ts file extension (that's implied in Typescript))
      • Source: Another answer here, here, and here, including this github issue
    7. In my case, the above didn't fix my problems. I did many of the steps below, eventually the problem stopped. I'm not sure which one did the trick:
      • Ensure any ts file which uses the problematic Pipe/Component, lives in an NgModule which imports the NgModule that defined the Pipe/Component!

        • For example if AModule, defines AComponent, which uses BPipe, defined in BModule, then AModule must import the BModule
          • In my case, I re-arranged my NgModule dependency tree. I used a "shared module" pattern so my Pipe could be re-used in multiple places.
      • Ensure there are no circular NgModule dependencies, like in these examples

      • If you are able, Upgrade NodeJS itself and/or angular and its tools (angular-cli, etc)

        • I ran into unrelated issues, so I found this list of suggestions... but these suggestions fixed other issues, and let me re-approach the Cannot determine the module... issue (which stopped happening!)
      • Be careful about using index.ts files aka "barrel files", and the order of exports

        • (This suggestion may help with index.ts file issues)
      • A general problem-solving approach : Remove / comment-out any references to the problematic file,

        • Remove the references one-at-a-time, from each file, until your angular app build works. If removing from a specific file solves the problem, you know which file is the source of the problem
        • If you remove the references from all files (which may be too hard to do; for me it was not too many/ not too hard); then your angular app should build...
        • Confirm your angular app still builds. In my case, my angular build had unrelated errors which I could easily fix (for example, Can't bind to '___' since it isn't a known property of '___', or Property '___' does not exist on type '___'. Did you mean '____', or Expected 0 arguments, but got 1.)
        • After fixing any other problems, you can uncomment your code gradually, while ensuring your ng build works. When I uncommented/added all my code back... my ng build no longer had the Cannot determine the module... issue

提交回复
热议问题