Error: No provider for t

Viewed 38750

I am trying to compile my angular code to production mode via ng build --prod and its successful but in browser I get the error :

ERROR p {__zone_symbol__error: Error: Uncaught (in promise): Error: NullInjectorError: No provider for t!

Error: NullInjectorError:…, …}

where as when I run just ng build then the html works fine.

I have tried several links mentioned on git hub:

https://github.com/angular/angular/issues/19219 https://github.com/angular/angular-cli/issues/6851

Doesn't help much.

Attaching my Package.json file content:

{
  "name": "xxx",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "test": "ng test",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "protractor",
    "build": "ng build && node server.js",
    "dev": "ng build -w & nodemon server.js --watch dist --watch server",
    "production": "ng build --prod & node server.js dist server "
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~5.0.0",
    "@angular/common": "~5.0.0",
    "@angular/compiler": "~5.0.0",
    "@angular/compiler-cli": "~5.0.0",
    "@angular/core": "~5.0.0",
    "@angular/forms": "~5.0.0",
    "@angular/http": "~5.0.0",
    "@angular/platform-browser": "~5.0.0",
    "@angular/platform-browser-dynamic": "~5.0.0",
    "@angular/router": "~5.0.0",
    "@jaspero/ng2-alerts": "0.0.7",
    "axios": "^0.17.0",
    "body-parser": "^1.18.2",
    "cookie-parser": "^1.4.3",
    "core-js": "^2.4.1",
    "ethereumjs-tx": "^1.3.3",
    "express": "^4.16.2",
    "express-session": "^1.15.6",
    "mongoose": "^4.12.6",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "web3": "^1.0.0-beta.24",
    "web3-provider-engine": "^13.3.3",
    "zone.js": "^0.7.2"
  },
  "devDependencies": {
    "@angular/cli": "^1.5.0",
    "@angular/compiler-cli": "1.5.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.42",
    "codelyzer": "~2.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "~4.0.13",
    "ts-node": "1.2.1",
    "tslint": "^4.3.0",
    "typescript": "2.4.2"
  }
}

also app.module.ts

import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {JasperoAlertsModule} from '@jaspero/ng2-alerts';
// Imports commented out for brevity
import {RouterModule} from '@angular/router';
// Components
import {AppComponent} from './app.component';
import {LoginComponent} from './component/login/login.component';
import {DashboardComponent} from './component/dashboard/dashboard.component';
// Services
import {RecordsService} from './services/records.service';
import {AccountService} from './services/account.service';
import {AuthenticateService} from './services/authenticate.service';
import {FooterComponent} from './component/footer/footer.component';
import {HeaderComponent} from './component/header/header.component';


// Define the routes
const ROUTES = [
  {
    path: '',
    redirectTo: 'login',
    pathMatch: 'full'
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'dashboard',
    component: DashboardComponent
  }
];

@NgModule({
  declarations: [
    AppComponent,
    HeaderComponent,
    FooterComponent,
    LoginComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    BrowserAnimationsModule,
    FormsModule,
    JasperoAlertsModule,
    RouterModule.forRoot(ROUTES) // Add routes to the app
  ],
  providers: [
    RecordsService,
    AccountService,
    AuthenticateService
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

11 Answers

You are trying to use a service that is not listed in providers of your AppModule. Add the service to a providers list to make it work.

Run the following command and first find which service dependency is missing. Fix it locally, Prod build will automatically works.

ng serve --prod --optimization=false

Disable the terser plugin by editing the file:

node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/common.js

and comment out:

extraMinimizers.push(new TerserPlugin({
  sourceMap: scriptsSourceMap,
  parallel: true,
  cache: true,
  terserOptions,
}));

rebuild the app with ng build frontend --prod and you should be able to see in the console which service can't be injected.

One of my teammate faced this. He was missing HttpClientModule in imports in app.module.ts

If you are facing import issues in module file, Run this code

ng serve --configuration=staging --optimization=false
OR
ng serve --configuration=prod--optimization=false

This code will help you to find which module is not imported in Module file.
In my case I missed importing MatSnackBarModule and BrowserModule

if you use HttpClient import HttpClientModule in your app.module

  .
  .
  .
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    HttpClientModule  // <--- add this line
  ],

I did not want to give up the optimization as the bundle size became about 8 times larger.

luckily it turned out to be something stupid :) I had providers:[] section in one of my components When I removed it the error has disappeared.

Hope this might be helpful for someone!

For Error: NullInjectorError: No provider for t! (note: only for the error the provider is "t", if it is another provider it may be for not having declared app.module -> provider [])

The problem occurred when I compiled with ng "ng build --prod", switched to "npm run build --prod" and the problem disappeared.

try to compile with another version or with npm

I had this "Error: NullInjectorError: No provider for t!" problem because I used FormBuilder before importing ReactiveFormsModule in app.module

For me, I got this error when building a Chrome Extension with Angular.

The problem: ngIf= instead of *ngIf=.

Related