Encountered an error when incorporating nguniversal/express-engine into an Angular project: "Unable to locate the BrowserModule import in /src/app/app.module.ts"

One of the initial questions

Purpose

The main aim is to integrate SSR into my Angular project using

ng add @nguniversal/express-engine --clientProject [name]
(to enable dynamic prerendering of meta tags).

Expected Outcome

I anticipated the command to run smoothly, setting up all the necessary structures and updating existing files as detailed in this YouTube tutorial.

Actual Outcome

Instead, the console output displays:

Installing packages for tooling via npm.
Installed packages for tooling via npm.
Cannot find BrowserModule import in /src/app/app.module.ts

Even though BrowserModule is imported in app.module.ts.

Attempted Solutions

  1. Reinstalling the package

I attempted uninstalling the package using

npm uninstall @nguniversal/express-engine
and re-executing the aforementioned ng add, but the issue persisted.

Similar concerns raised by others regarding ng adding @nguniversal/express-server do not seem relevant in this case. While they were able to generate some scaffolding and new files, I am unable to create any files at all; although the module did get added to my node_modules directory.

Could the problem lie in a possible misinterpretation of typescript within app.module.ts? The BrowserModule import statement is present within the file and included in the imports array. Here is the output for npm ls typescript:

+-- @angular-devkit/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="caa8bfa3a6aee7aba4adbfa6abb88afae4f3fafbe4f2">[email protected]</a>
| `-- @angular-devkit/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3351465a5f571e5c43475a5e5a49564173031d0a03021d0b">[email protected]</a>
|   `-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13676a63766070617a636753203d253d26">[email protected]</a>
+-- @ng-toolkit/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a7bcbba4b7a0a1b3be92e3fce3fce0e3">[email protected]</a>
| +-- @ng-toolkit/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="da85afaeb3b6a99aebf4ebf4efeb">[email protected]</a>
| | `-- @schematics/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e1808f86948d8093a1d6cfd2cfd0d1">[email protected]</a>
| |   `-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a3e333a2f392938233a3e0a796478647e">[email protected]</a>
| `-- @schematics/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7918171e0c15180b3949574f5741">[email protected]</a>
|   `-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02767b72677161706b727642302c352c30">[email protected]</a>
`-- <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bacec3cadfc9d9c8d3cacefa8994829489">[email protected]</a>

Additional Information (for David)

  1. app.module.ts
    import { BrowserModule, Meta, Title } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    
    import { AppRoutingModule } from './app-routing.module';
    import { AppComponent } from './app.component';
    import { HeaderComponent } from './header/header.component';
    import { FooterComponent } from './footer/footer.component';
    import { HomeComponent } from './home/home.component';
    import { FontAwesomeModule } from '@fortawesome/angular-fontawesome';
    import { SoftwareComponent } from './software/software.component';
    import { MediaComponent } from './media/media.component';
    import { ShopComponent } from './shop/shop.component';
    import { FilmDetailsComponent } from './film-details/film-details.component';
    import { ShareModalComponent } from './share-modal/share-modal.component';
    import { ShareModule } from 'ngx-sharebuttons';
    import { ShareButtonModule } from 'ngx-sharebuttons/button';
    import { ShareIconsModule } from 'ngx-sharebuttons/icons';
    
    
    @NgModule({
      imports: [
        ShareButtonModule,
        ShareIconsModule // Optional if you want the default share icons
      ]
    })
    @NgModule({
      declarations: [
        AppComponent,
        HeaderComponent,
        FooterComponent,
        HomeComponent,
        SoftwareComponent,
        MediaComponent,
        ShopComponent,
        FilmDetailsComponent,
        ShareModalComponent
      ],
      imports: [
        BrowserModule,
        AppRoutingModule,
        FontAwesomeModule,
        ShareModule,
        ShareButtonModule,
        ShareIconsModule
      ],
      providers: [Meta, Title],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
  1. main.ts
    import { enableProdMode } from '@angular/core';
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    
    import { AppModule } from './app/app.module';
    import { environment } from './environments/environment';
    
    if (environment.production) {
      enableProdMode();
    }
    
    platformBrowserDynamic().bootstrapModule(AppModule)
      .catch(err => console.error(err));

Answer №1

The reason for this issue is due to having multiple NgModules in the app.module file, where the initial NgModule does not include BrowserModule.

If you eliminate the first NgModule and keep the modules imported in the second one, the application should still function correctly without any issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to pass form values from child components to parents when utilizing a value accessor implementation?

What is the most effective method to retrieve the values of a child component control group from a parent component? I have currently implemented this using value accessor, but I am curious about the best approach. You can check out the plunker demo I have ...

The color scheme in Visual Studio 2019 for Angular and Typescript code is quite unappealing

Currently following a Udemy course on Angular (Using Angular, Angular Material, Angularfire (+ Firebase with Firestore), and NgRx to create a functional Angular App). Instead of using VS Code, I decided to use Visual Studio 2019 to become more accustomed ...

Tips for simulating difficult private attributes within a class during unit testing in TypeScript

Is there a way to mock the value of a hard private property in a unit test? For example, how can I expect something like expect(event.getEventHis()).toBeEqual(['a', 'b']) export class EventController { #event: []; constructor() { ...

What strategies can I implement to reduce the size of my Angular application to 500K or less?

I have been researching ways to reduce the size of my Angular application, but have not yet found a solution that significantly decreases its size. Currently, my application is 4M in production and 14M in development. So far, I have tried: Lazily loadin ...

What are the steps for implementing persisting and rehydrating data in redux-toolkit?

After setting up the redux-persist with react-toolkit as recommended in the documentation, I found myself needing to perform some operation on rehydrate. Unfortunately, my attempts have been unsuccessful so far. Here is what I have tried: ... import { RE ...

How can you proactively rebuild or update a particular page before the scheduled ISR time interval in Next.js?

When using NextJS in production mode with Incremental Static Regeneration, I have set an auto revalidate interval of 604800 seconds (7 days). However, there may be a need to update a specific page before that time limit has passed. Is there a way to rebui ...

I am struggling to comprehend the concept of dependency injection. Is there anyone available to provide a clear explanation for me?

I am working on a NestJS application and trying to integrate a task scheduler. One of the tasks involves updating data in the database using a UserService as shown below: import { Injectable, Inject, UnprocessableEntityException, HttpStatus, } fro ...

What is the proper way to define the type for a functional React component by using object destructuring on props?

As I continue to learn TypeScript and work on declaring advanced types, I am faced with converting my CRA project to TypeScript. Within this project, I have a component that closely resembles examples from react-router-dom, but I have not come across any T ...

What is the method for locating an element within an array?

The content being returned is presenting a challenge. How can I retrieve data from inside 0? I attempted to access it using date[0] without success const { data } = getData(); The result of console.log(data) is shown below: enter image description here ...

Formatting numbers in Angular 4 to display in Million or Thousand format

I need assistance with formatting numbers in my Angular 4 application. I want to display the number in a certain format based on its value. For example, if the number is 12.23 million, it should be displayed as 12.2M (with one decimal place). If the numbe ...

Is there a way to view all properties, including inherited ones, in nodejs?

When I am working with nodejs, one of my favorite techniques is to use console.log in order to see the available data within an object. However, I've noticed that this method doesn't work well with inherited properties: var Person = function () ...

Having trouble resolving all parameters for my directive in Angular 2

Currently, I am working on a directive that relies on TemplateRef<any> and ViewContainerRef as dependencies. However, when trying to inject these dependencies into my directive, it fails to do so. Below is the code snippet I'm working with: mai ...

What is the best way to include a package based on the version of node.js being used?

Is it possible to set a specific minimum version of node for a package to install without warning? There is a question regarding only installing the latest compatible version of a package. In my situation, I need my package to work with hardware that can& ...

What is the best way to programmatically generate a service within Angular?

Is there a way to dynamically create services at runtime using a string name (like reflection)? For example: let myService = new window[myClassName](myParams); Alternatively, you could try: let myService = Object.create(window[myClassName].prototype); m ...

Access a static class property through an instance

New and Improved Question: As I develop a client-side application, I am structuring an event-handling system inspired by the Redux framework. I have defined different event types as subclasses of a custom Event class. Each subclass includes its own stat ...

What steps can be taken to address the npm module vulnerability identified as [email protected] within curly brackets?

Is there a way to eliminate the vulnerability found in npm audit? What is the process for updating the chokidar module? How can one update a dependent module without affecting the parent module? Manual Review ...

The 'next-auth/react' module is missing when creating a custom email sign-in page using next-auth

Currently, I'm developing a NextJs application with next-auth to handle the authentication process. I have successfully implemented Email Sign In using next-auth's default pages. However, my goal now is to create a custom sign-in page. Following ...

Error encountered while creating SvelteKit: The module 'intl-messageformat' that was requested is in CommonJS format

Encountering an error during production build. Here's the output of npm run build executed on Node v16.20.1 npm run build > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c7a6c6a317a757278796e5c2c322c322d">[email&# ...

Is it normal that aws-eventbridge-lambda does not generate a Lambda function?

I've been experimenting with the AWS CDK (Typescript) to enhance my knowledge. I'm interested in setting up a lambda function to run daily at a specific time or at intervals of N minutes. Since I anticipate having multiple functions, it would be ...

Is it possible for a d3 chart to render twice in one area if it's rendered in two different places?

When attempting to showcase two distinct d3 pie charts on my webpage within individual mat-cards, they both end up displaying in the svg tag of the first d3 chart in my code. This is what my code looks like: <section class="three"> <! ...