Cant export service from module 'it was neither declared nor imported'

Code Spirit picture Code Spirit · May 23, 2017 · Viewed 39.8k times · Source


I am trying to export an Service from one of my modules but i only get the following error:

ERROR Error: Uncaught (in promise): 
Error: Can't export value ConfirmDialogService from SharedModule as it was neither declared nor imported!

My Module is the following:

import { NgModule }                 from "@angular/core";
import { CommonModule }             from "@angular/common";
import { FormsModule }              from "@angular/forms";
import { RouterModule }             from "@angular/router";
import { MaterialModule }           from "@angular/material";

import { ConfirmDialogComponent }       from './confirm-dialog/confirm-dialog.component';
import { ConfirmDialogService }         from './confirm-dialog/confirm-dialog.service';

@NgModule({
    imports: [
        RouterModule,
        CommonModule,
        MaterialModule,
        FormsModule
    ],
    providers: [
        ConfirmDialogService
    ],
    declarations: [
        ConfirmDialogComponent 
    ],
    exports: [
        ConfirmDialogComponent 
        ConfirmDialogService
    ]
})
export class SharedModule {}

The files do exist and are referenced correctly in TS but when running the app the error appears.

Answer

Günter Zöchbauer picture Günter Zöchbauer · May 23, 2017

You don't need to list services in exports, and you can only list components, directives, and pipes. For services, providers is relevant, but otherwise a TypeScript import is enough.