[Fixed] How to import a service from another module in Angular 6


Tech: using angular 6, angular cli and typescript.


I have an app module and a core module

I want my app modules components to use the services from my core module.

My core module contains a list of services:

import { NgModule } from '@angular/core';

// Services
import { AuthService } from './services/auth.service';

  declarations: [
  imports: [
  providers: [
export class CoreModule { }

This is my app module:

import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';

// Modules
import { CoreModule } from './core/core.module';

// Sections
import { COMPONENTS } from './components/index';
import { ROUTES } from './app.routes';

  declarations: [
  imports: [
  providers: [
  bootstrap: [ AppComponent ]
export class AppModule { }

Note: As you can see, I’m importing the CoreModule.

This is my component from the app module (its calling the core module to get the AuthService):

import { Component } from '@angular/core';

// This Auth Service import not working (says service not exported)
import { AuthService } from '../../core/core.module';

  selector: 'app-contact',
  templateUrl: './contact.component.html'
export class ContactComponent {

    private authService: AuthService
  ) {}

Issue: I’m trying to import a service into my app modules component called contact component like so:

import { AuthService } from '../../core/core.module';

Error I get:

Service not an exported member of core module.

Outcome: I want my contact component from app module to import the core module service called AuthService.


Since you’d imported your CoreModule in AppModule where you’d also decalred your ContactComponent (seems in COMPONENTS), both service and component belong to the same Module here, so they can see each other. What’ll have to do in your ContactComponent is merely import the service AuthService from its real location (not ../../core/core.module ).

import { AuthService } from ' <...>/auth.service';

Leave a Reply

(*) Required, Your email will not be published