GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ng add fails to find app.module.ts when using custom tsconfig.json paths · Issue #12740 · angular/angular-cli

ng add fails to find app.module.ts when using custom tsconfig.json paths #12740

New issue
Copy link
New issue
Copy link
Open
Bug
Open
ng add fails to find app.module.ts when using custom tsconfig.json paths#12740
Bug
Copy link
Labels
area: @schematics/angularfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix
Milestone
Backlog

Description

@seangwright
seangwright
opened on Oct 24, 2018
Issue body actions

Bug Report or Feature Request (mark with an x)

- [X ] bug report -> please search issues before submitting
- [X ] feature request

Whether this is a bug or a feature depends on whether or not custom paths in tsconfig.json are supported with the Cli.

Command (mark with an x)

- [X ] add

Versions

Angular CLI: 7.0.2
Node: 10.8.0
OS: win32 x64
Angular: 7.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Repro steps

  • ng new app-name (pick defaults)
  • Add "paths": { "@app/*": ["src/app/*"] } to tsconfig.json
  • Update main.ts to use this new path
    import { AppModule } from '@app/app.module';
    import { environment } from './environments/environment';
  • ng add @angular/material
  • Select Yes for ? Set up browser animations for Angular Material?

Repository all set up to reproduce this error is available here.
Just clone and type ng add @angular/material.

The log given by the failure

Could not read Angular module file: /src/@app/app.module.ts

Desired functionality

Cli handles custom paths in tsconfig.json.

Mention any other details that might be useful

Here is where material tries to get the app.module.ts path
https://github.com/angular/material2/blob/985774a4eaa14d1dcbf1ad96ab176043d38f433e/src/lib/schematics/ng-add/setup-project.ts#L56

Here is where the path to app.module.ts is being calculated in the schematics code

angular-cli/packages/schematics/angular/utility/ng-ast-utils.ts

Line 78 in 82f2bda

export function getAppModulePath(host: Tree, mainPath: string): string {

I believe the above link is what is causing the problem.

I've always preferred absolute urls in Angular apps compared to the relative paths (which become very difficult to read for nested components).

Adding custom paths to tsconfig.json works pretty well to solve this problem. VSCode works fine with it and the Cli has no other issues I've run into.

If using paths isn't officially supported does the Angular team recommend using relative paths like import { MyApiService } from '../../../../../my-api.service'?

Reacted by Joel, Tobias Haimerl, Qualwebs, Lachlan Heywood, Will, Michael Prentice, Martin Smith, Kent, Sugam Karki, Andrej Souček and 1 more
Reacted by Sergiobop

Activity

  1. alan-agius4 commented on Oct 25, 2018

    @alan-agius4
    alan-agius4
    on Oct 25, 2018
    Collaborator
    More actions

    Relates to
    #12729

    Reacted by Sean G. Wright
    Reacted by Nam Pham
  2. alan-agius4
    added
    freq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarely
    severity3: broken
    area: @schematics/angular
    on Oct 25, 2018
  3. ngbot
    added this to the needsTriage milestone on Oct 25, 2018
  4. alan-agius4
    added
    type: bug/fix
    on Oct 25, 2018
  5. ngbot
    modified the milestones: needsTriage, Backlog on Oct 25, 2018
  6. clydin
    added
    triage #1
    on May 27, 2020
  7. serridavide commented on Nov 30, 2020

    @serridavide
    serridavide
    on Nov 30, 2020
    More actions

    Did anyone find a workaround for this issue? I'm trying to add angular material to an existing project and run into this issue. Can someone help me to get it working?

    Reacted by Oleksandr Yefymov, Kent and Mohammad Jawad (Kasir) Barati
  8. Splaktar commented on Feb 22, 2021

    @Splaktar
    Splaktar
    on Feb 22, 2021
    Contributor
    More actions

    I just wanted to bump this and say that with Nx and people learning TS more, these custom path mappings are very common. I would suggest moving the frequency of this issue up from low to medium.

    This is something that Angular Material users have been running into for 3 years now when using our ng add schematic.

    Also based on my investigation in #20118, this is still occuring with 11.2.1.

  9. alan-agius4 commented on Feb 23, 2021

    @alan-agius4
    alan-agius4
    on Feb 23, 2021
    Collaborator
    More actions

    I had a look at this and continuing on what @Splaktar mentioned in #20118, in https://github.com/angular/components/blob/8f558eeb7e475da54ad69e85ec07229fb4b0c3e9/src/cdk/schematics/utils/ast/ng-module-imports.ts#L16 the tree.read is invoked with an inexistent path. Schematics are not aware of specific TypeScript path mappings logic as it works similar to the Node.JS file system and hence it is correctly failing.

    The module needs to be provided prior to passing it to tree.read. Since, the module resolution logic, is within the Angular Components schematic, it does appear to me that this is more of an Angular Material bug, where path mappings are not supported by the ng-add schematic.

    I did a high level example on what needs to be done in the schematic:

    const workspace = await getWorkspace(host);
    const project = getProjectFromWorkspace(workspace, options.project);
    let appModulePath = getAppModulePath(host, getProjectMainFile(project));
    
    if (!host.exits(appModulePath)) {
      const tsConfig = project.targets.get('build')?.options?.tsConfig;
      if (typeof tsConfig === 'string') {
        const { config } = ts.readConfigFile(tsConfig, host.read);
        const { resolvedModule } = ts.resolveModuleName(appModulePath, getProjectMainFile(project), config.options, {
          readFile: function (fileName: string): string {
            return host.read(fileName).toString().replace(/^\uFEFF/, '');
          },
          directoryExists: function (directoryName: string): boolean {
            // When the path is file getDir will throw.
            try {
              const dir = host.getDir(directoryName);
    
              return !!(dir.subdirs.length || dir.subfiles.length);
            } catch {
              return false;
            }
          },
          fileExists: function (fileName: string): boolean {
            return host.exists(fileName);
          },
          realpath: function (path: string): string {
            return path;
          },
          getCurrentDirectory: function () {
            return host.root.path;
          },
        });
    
        appModulePath = resolvedModule.resolvedFileName;
      }
    }

    Alternatively, for a simpler approach an addition option can be added that would allow the users to provide the module path.

    ng add @angular/material --module /src/app/app.module.ts
    
    const workspace = await getWorkspace(host);
    const project = getProjectFromWorkspace(workspace, options.project);
    let appModulePath = options.module ?? getAppModulePath(host, getProjectMainFile(project));
  10. alan-agius4
    removed
    triage #1
    on Jun 24, 2022
  11. ThisIsIvan commented on Jun 21, 2023

    @ThisIsIvan
    ThisIsIvan
    on Jun 21, 2023
    More actions

    Did anyone find a workaround for this issue? I'm trying to add angular material to an existing project and run into this issue. Can someone help me to get it working?

    Found a temporary solution: In your main.ts change the import path of AppModule.

    For me it looks like this:
    import { AppModule } from '@app/app.module'; -> import { AppModule } from './app/app.module';

    Absolute path didn't work:

    import { AppModule } from 'src/app/app.module';
    Could not read Angular module file: /src/src/app/app.module.ts
    
    Reacted by drmencos and tbrix84
  12. Sergiobop commented on Nov 19, 2023

    @Sergiobop
    Sergiobop
    on Nov 19, 2023
    More actions

    This still happens on Angular 17

    Setup to fail:

    • In your tsconfig.json add a custom path:
    ...
        "paths": {
          "@app/*": ["app/*"],
    ...
    
    • Then use it in your main.ts:
    import { bootstrapApplication } from '@angular/platform-browser';
    
    import { AppComponent } from '@app/app.component';
    import { appConfig } from '@app/app.config';
    
    bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
    
    • For example, try to add ssr package:
      ng add @angular/ssr

    The command fails:

    Cannot statically analyze bootstrapApplication call in src/main.ts

    • If you change main.ts to:
    import { bootstrapApplication } from '@angular/platform-browser';
    
    import { AppComponent } from './app/app.component';
    import { appConfig } from './app/app.config';
    
    bootstrapApplication(AppComponent, appConfig).catch((err) => console.error(err));
    

    It works @alan-agius4

  13. AngelCorre commented on Dec 29, 2023

    @AngelCorre
    AngelCorre
    on Dec 29, 2023
    More actions

    By default Angular 17 create a project that use standalone.
    You can change that putting false when you create your project.
    ng new nameproject --standalone=false

    Reacted by Diego Luiz Brum
  14. JonasDev17 commented on Jan 22, 2024

    @JonasDev17
    JonasDev17
    on Jan 22, 2024
    More actions

    I can confirm this. I'm having the same issue right now:

    Cannot statically analyze bootstrapApplication

    this happens when I run:

    ng add @angular/ssr

    EDIT:

    I had to change:

    import { appConfig } from "app/app.config";

    to:

    import { appConfig } from "./app/app.config";

    adding a ./ to the path

    Reacted by Daniel Naranjo, rems, Daniel Ehrhardt, Daniel Correia, JayPansuriyaSF, L. Papa, Hazem Karoui, Mohamed Birali and tbrix84
  15. STotev commented on Feb 9, 2024

    @STotev
    STotev
    on Feb 9, 2024
    More actions

    6 years later and still hasn't been fixed. I can confirm the same behaviour.

    Updated the path in main.ts only for the AppModule and finished successfully.

    Reacted by Ivan Skiridomov and Daniel Correia
  16. mbirali commented on Jan 16, 2025

    @mbirali
    mbirali
    on Jan 16, 2025
    More actions

    If you still have the same issue with Angular 19, as @JonasDev17 said

    Your main.ts file should look like:

    import { bootstrapApplication } from '@angular/platform-browser';
    import { AppComponent } from './app/app.component';
    import { appConfig } from './app/app.config';
    bootstrapApplication(AppComponent, appConfig).catch((err) =>
    console.error(err)
    );

    And NOT:

    import { bootstrapApplication } from '@angular/platform-browser';
    import { AppComponent } from 'app/app.component';
    import { appConfig } from 'app/app.config';
    bootstrapApplication(AppComponent, appConfig).catch((err) =>
    console.error(err)
    );

    The difference is ./

  17. kanehjeong commented on May 8, 2025

    @kanehjeong
    kanehjeong
    on May 8, 2025
    More actions

    Getting this error trying to add @angular/pwa even after copying the other people's examples above line for line.

    Reacted by elsantoalcielo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: @schematics/angularfreq1: lowOnly reported by a handful of users who observe it rarelyOnly reported by a handful of users who observe it rarelyseverity3: brokentype: bug/fix

    Type

    Bug

    Projects

    No projects

      Milestone

      • BacklogNo due date

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.