23

I have a simple app created with Angular-CLI, I wanted to refactor and move the app.component.ts code in separate component file and started to get a nasty error:

Module not found: Error: Can't resolve on './sb/sb.component.html' ( my SBComponent). Under the

This what i have:

app.module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

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

@NgModule({
  declarations: [
    AppComponent, SBComponent  
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component:

import { Component } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent  {


}

new component:

import { Component, OnInit } from '@angular/core';
import { NgModule,ElementRef, ViewChild, AfterViewInit, AfterContentInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'


@Component({

    selector: 'app-sb',
    templateUrl: './sb/sb.component.html'
})
export class SBComponent  {



}

Any help would be greatly appreciated!

7
  • 1
    sb.component.html exist inside sb folder ? if they are both in the same folder then try this ./sb.component.html Mar 4, 2017 at 5:24
  • Yes it exists within the sb folder, app.ts exists outside that folder. Mar 4, 2017 at 5:26
  • 4
    inside the SBComponent change the templateUrl to templateUrl: './sb.component.html' Mar 4, 2017 at 5:30
  • Thank u! That worked! I gave one vote, does that count as answered? Mar 4, 2017 at 5:40
  • 1
    @eagleEye glad it helped :) Mar 7, 2017 at 14:26

7 Answers 7

33

If your component is inside the same directory, then you need to point your templateUrl to the same folder as well. Here is how you can do it:

@Component({
    selector: 'app-sb',
    templateUrl: './sb.component.html'
})
8

This problem is very well known, when we are using component relative paths for external html and css files. The problem of absolute path and relative path can be resolved just by adding metadata ModuleId in component.

Now what does ModuleId do? ModuleId contains the absolute URL of the component class [when the module file is actually loaded]

This ModuleId value is used by the Angular reflection processes and the metadata_resolver component to evaluate the fully-qualified component path before the component is constructed

It's very easy to use. Just add one more entry in @Component decorative. Full code example is below.

 @Component({
  moduleId: module.id,    // fully resolved filename; defined at module load time
  selector: 'app-sb',
  templateUrl: 'sb.component.html' //Observe this.
})
export class SBComponent  {

}
1
  • 2
    This is worked for me: @Component({ moduleId:module.id, selector: 'my-demo', templateUrl: './input-text-validation.html' }) Jan 8, 2018 at 16:30
5

My problem was the same as questioned here, my solution was quite different

code that was causing problem:

@Component({
    selector : 'app-header',
    templateUrl : './header.component.html',
    styleUrls : ['']
})

here by changing :

styleUrls : ['']

to this:

styles : ['']

helped me remove module not found error, thought someone might get this error due to this silly mistake, that's why shared my solution

2
  • I was using an in-line template and therefore had to change templateUrl to template also -- thanks for your answer; found this quickly
    – Schalton
    Jul 28, 2020 at 3:10
  • In my case, this issue was also happening along with other problems. I've posted the answer here: stackoverflow.com/a/74994465/806202 Jan 3 at 14:09
3

Try to give relative path instead of absolute one.

It will fix the Solution.

Like : templateUrl = '../employee/employee.html' in your component.ts

0

Ok, above solutions are excellent and must be used to see if it fixes your issue. But the same error message can be displayed if you have set the Template property. I had the issue when I created the component using ng g c and then it had templateUrl but I was writing some test html inside of templateUrl, thinking it is template. Hope this helps

0

Something a react developer showed me. Go to your tsconfig.json and inside the outer curly braces {} where you have all the json options and codes, put this

"include": [
    "src/app"
]
//if all your components files are in app, you can do this so as to make importation easy. You can just call the component. It should work for you.

from here you can easily use import without much slashes / and dots. like in my code,

import { Component, OnInit, Input, Injectable, NgModule } from '@angular/core';
import { products } from "product";
//instead of import { products } from ".../../product"; which will still import but might show error.
import { FormsModule } from '@angular/forms';




@Component({
  selector: 'app-product-list',
  templateUrl: './product-list.component.html',
  styleUrls: ['./product-list.component.scss'],
  providers:  [
    FormsModule
  ]
})



export class ProductListComponent implements OnInit {

 products = products;


onNotify() {
  window.alert("You will be notified when the product goes on discount")
}

share() {
  window.alert('The Product Has Been Added To Your Cart')
}


  constructor() {

   }

  ngOnInit(): void {
  }

}
0

For me change styleUrls : [''] from

@Component({
        selector : 'app-header',
        templateUrl : './header.component.html',
        styleUrls : ['']
    })

into styles: [''] solve this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.