Skip to content

ng add @angular/pwa cannot find AppModule if imported through index.ts #11499

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
madmath03 opened this issue Jul 8, 2018 · 4 comments
Open
Labels
area: @angular/pwa freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Milestone

Comments

@madmath03
Copy link

Bug Report or Feature Request (mark with an x)

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

Area

- [ ] devkit
- [x] schematics

Versions

$ node --version
v8.11.2
$ npm --version
5.6.0

Repro steps

  • Create an Angular 6 application
  • If not present, create a index.ts in the App module folder with at least the following content:
export * from './app.module';
  • In the main.ts, import the App module indirectly by using its parent folder:
import './polyfills.ts';

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app'; // works because ./app/index.ts exports AppModule

if (environment.production) {
    enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);
  • Now use angular CLI to add PWA
$ ng add @angular/pwa --project <your-project-name-from-angular-json>

The log given by the failure

Installing packages for tooling via npm.

+ @angular/pwa@0.6.8
updated 1 package in 11.487s
Installed packages for tooling via npm.
Could not read file (/src/app.ts).

Desired functionality

Make PWA install work even when importing the AppModule indirectly (through an index.ts).

To be more accurate, the findBootstrapModulePath function from ng-ast-utils.d.ts should properly identify the AppModule path in this setup.

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

This method is called by updateAppModule from

const modulePath = getAppModulePath(host, mainPath);

The failure occurs when trying to update the content of a file which does not exists. Checking the file exists and displaying a better message would also help.

Mention any other details that might be useful

The issue was encountered when attempting to add PWA to our NGXP Seed web side.

$ git clone https://github.com/Monogramm/ngxp-seed.git
$ git checkout develop
$ ng add @angular/pwa --project web

Since we managed to find out the root cause, the workaround of using the complete path will be used for now.

@bogacg
Copy link

bogacg commented Jul 13, 2018

I had similar problem. In my case I'm using tsconfig path mapping and reference AppModule in main.ts with that reference:

import { AppModule } from '@app/app.module';

I get the same type of error: Could not read file (/src/@app/app.module.ts).

Changing back only that reference to import { AppModule } from './app/app.module'; in main.ts results execution of ng add @angular/pwa as expected

@madmath03
Copy link
Author

We went for the same solution of import { AppModule } from './app/app.module';.

Knowing this workaround makes the issue less critical but it's still a bit weird to be forced to do this just to add a component 😉

@cedvdb
Copy link

cedvdb commented Oct 2, 2018

it also doesn't find it if you have alias for imports

@ngbot ngbot bot added this to the needsTriage milestone Jan 24, 2019
@kyliau kyliau added needs: investigation Requires some digging to determine if action is needed triage #1 type: bug/fix labels May 27, 2020
@kasir-barati
Copy link

kasir-barati commented Jun 20, 2022

Is this issue abandoned @kyliau? I faced in 2022. After 2 years this issue still exists 🤒?

image

You can reproduce the issue in this repo. Just do ng add @angular/material

BTW I hope this issues resolve. @angular-robot

@alan-agius4 alan-agius4 added freq1: low Only reported by a handful of users who observe it rarely severity3: broken and removed needs: investigation Requires some digging to determine if action is needed labels Sep 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: @angular/pwa freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Projects
None yet
Development

No branches or pull requests

7 participants