2
votes

I have been at this for 2.5 hours now and been thru multiple posts. Tried several suggestions on https://github.com/angular/angular/issues/15763, which frustrated me as the source of the above error seems different.

I am on angular 5 and on build, I get the error:

ERROR in : Unexpected value 'ToasterModule in .../node_modules/angular5-toaster/dist/bundles/angular5-toaster.umd.js' imported by the module 'AppModule in ../src/app/app.module.ts'. Please add a @NgModule annotation.

the library don't have its own copy of node_modules. Its own tsconfig.js file is:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "declaration": true,
    "stripInternal": true,
    "noUnusedLocals": false,
    "types": [ "jasmine", "node" ],
    "lib": ["es2015", "dom"]
  },
  "include": [
    "angular5-toaster.ts",
    "src/**/*"
  ],
  "exclude": [
      "node_modules",
      "bundles"
    ]
}

angular5-toaster/package.json (partial):

"devDependencies": {
    "@angular/animations": "4.1.0",
    "@angular/common": "4.1.0",
    "@angular/compiler": "4.1.0",
    "@angular/compiler-cli": "4.1.0",
    "@angular/core": "4.1.0",
    "@angular/platform-browser": "4.1.0",
    "@angular/platform-browser-dynamic": "4.1.0",
    "@angular/platform-server": "4.1.0",
    "@types/jasmine": "2.5.47",
    "@types/node": "7.0.12",
"module": "angular5-toaster.js",
  "name": "angular5-toaster",
  "optionalDependencies": {},
  "peerDependencies": {
    "@angular/common": ">=4.0.0 <=5.0.2",
    "@angular/compiler": ">=4.0.0 <=5.0.2",
    "@angular/core": ">=4.0.0 <=5.0.2",
    "rxjs": "^5.0.0-beta.11"
  },

My app.module.ts:

  import { NgModule} from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { Ng4LoadingSpinnerModule } from 'ng4-loading-spinner';
import {ToasterModule, ToasterService} from 'angular5-toaster';
@NgModule({
    imports: [
        CommonModule,
        BrowserModule,ToasterModule,
        BrowserAnimationsModule,
        Ng4LoadingSpinnerModule.forRoot(),

        AppRoutingModule
    ],
    declarations: [AppComponent],
    providers: [ToasterService
    ],
    bootstrap: [AppComponent, ]
})
export class AppModule {}

package.json:

{
  "name": "RLRC",
  "version": "4.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve --ec true",
    "build": "ng build --prod",
    "gitbuild": "ng build --prod --base /start-angular/RLRC/master/dist/",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.0.1",
    "@angular/common": "^5.0.1",
    "@angular/compiler": "^5.0.1",
    "@angular/core": "^5.0.1",
    "@angular/forms": "^5.0.1",
    "@angular/http": "^5.0.1",
    "@angular/platform-browser": "^5.0.1",
    "@angular/platform-browser-dynamic": "^5.0.1",
    "@angular/router": "^5.0.1",
    "@ng-bootstrap/ng-bootstrap": "^1.0.0-beta.5",
    "@ngx-translate/core": "^8.0.0",
    "@ngx-translate/http-loader": "^2.0.0",
    "chart.js": "^2.7.1",
    "core-js": "^2.5.1",
    "datatables.net": "^1.10.16",
    "font-awesome": "^4.7.0",
    "jquery": "^3.2.1",
    "ng2-charts": "^1.6.0",
    "ng4-loading-spinner": "^1.1.1",
    "ngx-treeview": "^2.0.1",
    "rxjs": "^5.5.2",
    "yarn": "^1.3.2",
    "zone.js": "^0.8.18"
  },
  "devDependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "^5.0.1",
    "@angular/language-service": "^5.0.1",
    "@types/jasmine": "~2.6.3",
    "@types/jasminewd2": "~2.0.3",
    "@types/jquery": "^3.2.17",
    "@types/node": "~8.0.51",
    "codelyzer": "~4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.2.0",
    "ts-node": "~3.3.0",
    "tslint": "~5.8.0",
    "typescript": "~2.4.2"
  }
}

I notice angular5-toaster (version 1.0.2) wasn't added to the list of dependence above but adding it didnt make any difference either.

What could be the problem? I would appreciate help as i might face same problem with other libraries.

3
you had selected wrong module for toaster use this module as this has large no of download and more stable than angular-5 toaster npmjs.com/package/ng2-toastrSantosh Singh
Emm. Thanks @SantoshSingh Lemme see solutions before i change anything libraries now. I was honestly hoping a solution than a solution cos i have no idea of ng2-toastr will cause me the same problem.Nie Selam
That did it @SantoshSingh. and they are the same anyway so it is nice.Nie Selam

3 Answers

2
votes

This seems to be a particular problem with AoT builds and angular5-toaster.

I solved this by switching to ng2-toastr which appears to be more stable and offer the same functionality.

0
votes

You should not have ToasterService in the 'providers' section of your app module. The ToasterModule provides the service.

However, I don't think that's your entire problem.

I put a simple working example of using the library on StackBlitz, at https://stackblitz.com/edit/angular-msm5nn

0
votes

Unfortunately, the accepted answer is convenient in the case when the use of the library is superficial, it is not deeply embedded in the project. In my case, transferring from angular5-toaster to ng2-toastr would take much time. So I found another solution which can help others. angular5-toaster suggests importing it as follow:

import {ToasterModule } from 'angular5-toaster';

I used direct approach:

import { ToasterModule } from './../../node_modules/angular5-toaster/angular5-toaster';