[SOLVED] Provider Problem: Runtime Error: Cannot find module


#1

Hello guys,

since I tried to use a provider, I have the following problem: I get the Runtime Error "Cannot find module “…/provider/global/global”, but the path is correct. I think my code is missing something like an import, but also after searching the entire web I can not find the problem.

This is my code:

Provider:
global.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';


@Injectable()
export class GlobalProvider {

public globalcount: number = 666;

  constructor(public http: HttpClient) {
    console.log('Hello GlobalProvider Provider');
  }

}

app.component.ts

import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { CentralPage } from '../pages/central/central';

import { GlobalProvider } from '../providers/global/global';

import * as math from 'mathjs';

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = CentralPage;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
      statusBar.styleDefault();
      splashScreen.hide();
    });
  } 
}

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';

import { MyApp } from './app.component';
import { CentralPage } from '../pages/central/central';

import { GlobalProvider } from '../providers/global/global';

@NgModule({
  declarations: [
    MyApp,
    CentralPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    CentralPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    GlobProvider
  ]
})
export class AppModule {}

and the page where I want to access the variable:
central.ts:

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AlertController } from 'ionic-angular';

import { GlobalProvider } from '../providers/global/global';

@Component({
  selector: 'page-central',
  templateUrl: 'central.html',
})
export class CentralPage {

  constructor(public navCtrl: NavController, public alertCtrl: AlertController, public global: GlobalProvider) {}

	globaltest(){
		console.log(JSON.stringify(global.globalcount));
	}	
}	

If you have any ideas what my problem is, I would be very thankfully for help.

Greetings,

Robert


#2

Hi@RobertLinde ,
do you getting the error on central.ts page?
I think the

import { GlobalProvider } from '../providers/global/global'(central.ts) is wrong
try
import { GlobalProvider } from '../../providers/global/global'(central.ts)

#3

Thanks for your answer!

Well, I am using the central.html page as rootpage so it just shows like this:

If I take everything concerning the provider out of the central.ts, I can start the app and do not get the error. I also tried to use an extra constructor for the Provider:

export class CentralPage {

  constructor(public navCtrl: NavController, public alertCtrl: AlertController) {}
  constructor(public global: GlobalProvider){}

	globaltest(){
		console.log(JSON.stringify(global.globalcount));
	}
}

Than I can start the app but if I want to use console.log to show the value of globalcount (it should be 666), it just shows “null” in the console…


#4
import { GlobalProvider } from '../../providers/global/global'

Should I use it only in the central.ts or also in app.module.ts and app-component.ts?


#5

Hi@RobertLinde
Your import in app.module.ts and app.component.ts are right.Need only change the import on central.ts
page


#6

I tried your suggestion but than I get this error:

Runtime Error
Uncaught (in promise): Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: NullInjectorError: No provider for HttpClient! Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: NullInjectorError: No provider for HttpClient! at _NullInjector.get (http://localhost:8100/build/vendor.js:1377:19) at resolveToken (http://localhost:8100/build/vendor.js:1675:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20) at resolveToken (http://localhost:8100/build/vendor.js:1675:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20) at resolveNgModuleDep (http://localhost:8100/build/vendor.js:11270:25) at _createClass (http://localhost:8100/build/vendor.js:11307:29) at _createProviderInstance$1 (http://localhost:8100/build/vendor.js:11281:26)
Stack
Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]:
StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]:
NullInjectorError: No provider for HttpClient!
Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]:
StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]:
NullInjectorError: No provider for HttpClient!
at _NullInjector.get (http://localhost:8100/build/vendor.js:1377:19)
at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
at resolveNgModuleDep (http://localhost:8100/build/vendor.js:11270:25)
at _createClass (http://localhost:8100/build/vendor.js:11307:29)
at _createProviderInstance$1 (http://localhost:8100/build/vendor.js:11281:26)
at c (http://localhost:8100/build/polyfills.js:3:19752)
at Object.reject (http://localhost:8100/build/polyfills.js:3:19174)
at NavControllerBase._fireError (http://localhost:8100/build/vendor.js:51699:16)
at NavControllerBase._failed (http://localhost:8100/build/vendor.js:51692:14)
at http://localhost:8100/build/vendor.js:51739:59
at t.invoke (http://localhost:8100/build/polyfills.js:3:14976)
at Object.onInvoke (http://localhost:8100/build/vendor.js:5134:33)
at t.invoke (http://localhost:8100/build/polyfills.js:3:14916)
at r.run (http://localhost:8100/build/polyfills.js:3:10143)
at http://localhost:8100/build/polyfills.js:3:20242


#7

import this on your global provider

import { Http } from '@angular/http;
constructor(public http: Http) {
  }

#8

Than my code looks like this:

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';


@Injectable()
export class GlobalProvider {

public globalcount: number = 666;

  constructor(public http: HttpClient) {
    console.log('Hello GlobalProvider Provider');
  }
    constructor(public http: Http) {
  }
}

But I still get the same error :frowning:


#9

Why did u duplicating the constructor.You can create all instance under single constructor
only use this

constructor(public http: Http){
}

remove this one from your code

import { HttpClient } from '@angular/common/http';
 constructor(public http: HttpClient) {
    console.log('Hello GlobalProvider Provider');
  }

#10
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';


@Injectable()
export class GlobalProvider {

 constructor(public http: Http) {
  }  
public globalcount: number = 666;
}

But also with this code for the global.ts, I still get the same error. Do I have to import the Http to the other pages as well??


#11

No need ,Please console the error.Let me check


#12
Error
Close
Runtime Error
Uncaught (in promise): Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: NullInjectorError: No provider for HttpClient! Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: NullInjectorError: No provider for HttpClient! at _NullInjector.get (http://localhost:8100/build/vendor.js:1377:19) at resolveToken (http://localhost:8100/build/vendor.js:1675:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20) at resolveToken (http://localhost:8100/build/vendor.js:1675:24) at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16) at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20) at resolveNgModuleDep (http://localhost:8100/build/vendor.js:11270:25) at _createClass (http://localhost:8100/build/vendor.js:11307:29) at _createProviderInstance$1 (http://localhost:8100/build/vendor.js:11281:26)
Stack
Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
Error: StaticInjectorError(AppModule)[GlobalProvider -> HttpClient]: 
  StaticInjectorError(Platform: core)[GlobalProvider -> HttpClient]: 
    NullInjectorError: No provider for HttpClient!
    at _NullInjector.get (http://localhost:8100/build/vendor.js:1377:19)
    at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
    at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
    at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
    at resolveToken (http://localhost:8100/build/vendor.js:1675:24)
    at tryResolveToken (http://localhost:8100/build/vendor.js:1617:16)
    at StaticInjector.get (http://localhost:8100/build/vendor.js:1485:20)
    at resolveNgModuleDep (http://localhost:8100/build/vendor.js:11270:25)
    at _createClass (http://localhost:8100/build/vendor.js:11307:29)
    at _createProviderInstance$1 (http://localhost:8100/build/vendor.js:11281:26)
    at c (http://localhost:8100/build/polyfills.js:3:19752)
    at Object.reject (http://localhost:8100/build/polyfills.js:3:19174)
    at NavControllerBase._fireError (http://localhost:8100/build/vendor.js:51699:16)
    at NavControllerBase._failed (http://localhost:8100/build/vendor.js:51692:14)
    at http://localhost:8100/build/vendor.js:51739:59
    at t.invoke (http://localhost:8100/build/polyfills.js:3:14976)
    at Object.onInvoke (http://localhost:8100/build/vendor.js:5134:33)
    at t.invoke (http://localhost:8100/build/polyfills.js:3:14916)
    at r.run (http://localhost:8100/build/polyfills.js:3:10143)
    at http://localhost:8100/build/polyfills.js:3:20242


Ionic Framework: 3.9.2
Ionic App Scripts: 3.1.9
Angular Core: 5.2.10
Angular Compiler CLI: 5.2.10
Node: 8.11.1
OS Platform: Windows 10
Navigator Platform: Win32
User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36

#13

Try these in your app.modules:

import { HttpModule } from ‘@angular/http’;
import { HttpClientModule, HttpClient } from ‘@angular/common/http’;


#14

This and adding

HttpClientModule,
HttpModule

to imports solved the problem! Also adding

import { Http } from '@angular/http';

to global.ts is needed.

Thank you guys!