reactjswebpackcreate-react-appwebpack-html-loader

React import html - Module parse failed: You may need an appropriate loader to handle this file type


I want to import a html file and convert into json.

const DocHtml = require('../../Shared/assests/index.html');
const template = { __html: DocHtml };

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/,
        use: {
          loader: 'html-loader',
          options: {
            attrs: [':data-src']
          }
        }
      },
    ],
  },
};

But it raise an error saying...

./src/User/Shared/assests/index.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
| <html xmlns="http://www.w3.org/1999/xhtml">
| <head>

Solution

  • After lots of research I figured it out.

    // Add this to react node-module webpack.config.js file
    module.exports = {
      module: {
        loaders: [
          { 
            test: /\.html$/,
            exclude: /node_modules/,
            include: /src/User/Shared/ + /assests/,
            loader: 'html-loader'
          }
        ],
      },
    };