The template specified for component AppComponent is not a string
Asked Answered
R

4

6

disclaimer: Yes, I saw that there are more "The template specified for component AppComponent is not a string" related questions but none of them describes the specific problem I'm experiencing.

I get this runtime error when I compile without AoT in ng build:

Uncaught Error: The template specified for component AppComponent is not a string

This error actually make sense because in the generated bundled code (main.ts) I see:

template: __webpack_require__(/*! raw-loader!./app.component.html */ "../node_modules/raw-loader/dist/cjs.js!../Scripts/app/app.component.html"),

while in a new Angular app I see:

template: __webpack_require__(/*! ./app.component.html */ "./src/app/app.component.html")

Somehow the raw-loader gets added as a loader to my .html files.

Now maybe it's important to mention that i'm migrating my webpack 4 AngularJs project to Angular 8. BUT When I debug my webpack build I see no rule that its test contains .html and a loader. that contains raw-loader.

Debug picture of loader rules

So my loaders rules doesn't affect this raw-loader addition to app.component.html

app.component.ts:

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

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

app.component.html:

 <div></div>

I'll appreciate any help, thanks.

Rugg answered 17/7, 2019 at 14:32 Comment(1)
post AppComponent.tsSyncopation
R
6

Downgrading raw-loader from 2.0.0 to 1.0.0 fixed this issue.

First I learned from angular source code that they added raw-loader to templateUrl by default in here since Angular 8. Then it's later used in here.

raw-loader 2.0.0 generated:

/***/ "../node_modules/raw-loader/dist/cjs.js!../Scripts/app/app.component.html":
/*!********************************************************************************!*\
  !*** ../node_modules/raw-loader/dist/cjs.js!../Scripts/app/app.component.html ***!
  \********************************************************************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony default export */ __webpack_exports__["default"] = ("<div>\r\n    app componenets works!\r\n</div>\r\n");

/***/ }),

And raw-loader 1.0.0 generates:

    /***/ "../node_modules/raw-loader/index.js!../Scripts/app/app.component.html":
/*!********************************************************************!*\
  !*** ../node_modules/raw-loader!../Scripts/app/app.component.html ***!
  \********************************************************************/
/*! no static exports found */
/***/ (function(module, exports) {

module.exports = "<div>\r\n    app componenets works!\r\n</div>\r\n"

/***/ }),

which is good. Angular needs module.exports to be string here.

Rugg answered 18/7, 2019 at 8:59 Comment(0)
S
2

Make sure you are using

  templateUrl: './app.component.html', // or whatever filename

instead of template: './app.component.html',

Syncopation answered 17/7, 2019 at 14:36 Comment(5)
I'm using it, I'll add the snippet of code to my question.Rugg
you are probably missing an ending quote in the html file , can you post app.component.html too ?Syncopation
I posted it alsoRugg
What is kibana & ng-view this is not valid HTMLSyncopation
I simplified app.component.html and it still happens, I updated my answer with the current app.component.htmlRugg
A
0

use to be

templateUrl: require('./app.component.html').default

..raw-loader > 1 , because export as default.

Alex answered 24/7, 2019 at 1:28 Comment(0)
S
0

Error occurs in the template of component AppComponent.

firstly close the browser and then reopen it after that type ( npm i ) into the terminal, after completion of npm installation. then type ( ng serve).

Swats answered 21/12, 2021 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.