6

I'm trying to load an HTML template using HtmlWebpackPlugin and it seems not working. If I try to load the plugin without arguments it works. Any ideas?

The index.html file that I'm trying to load is in the right place, just to consider

package.json:

{
 ...
  "devDependencies": {
    "@babel/core": "^7.7.4",
    "@storybook/html": "^5.2.8",
    "babel-loader": "^8.0.6",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "css-loader": "^3.2.1",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.13.0",
    "sass-loader": "^8.0.0",
    "style-loader": "^1.0.1",
    "ts-loader": "^6.2.1",
    "typescript": "^3.7.3",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "axios": "^0.19.0"
  }
}

webpack.config.js:

const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
  mode: 'development',
  entry: './src/index.ts',
  devtool: 'source-map',
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, 'dist')
  },

  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        use: [
          {
            loader: 'style-loader'
          },
          {
            loader: 'css-loader'
          },
          {
            loader: 'sass-loader'
          }
        ]
      },
      {
        test: /.(ts|tsx)?$/,
        loader: 'ts-loader',
        include: [path.resolve(__dirname, 'src')],
        exclude: [/node_modules/]
      }
    ]
  },
  plugins: [
    new webpack.ProgressPlugin(),
    new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'src/index.html'
    }),
    new MiniCssExtractPlugin({
      filename: '[name].css'
    })
  ],
  devServer: {
    hot: true,
    host: '0.0.0.0'
  },

  resolve: {
    extensions: ['.tsx', '.ts', '.js', '.scss', '.css']
  }
};

2 Answers 2

28

For me, the solution was to make sure everything was updated.

I'm on Webpack 5, and everything except html-webpack-plugin was up to date. I updated html-webpack-plugin from v4 to v5, and the issue went away.

5
  • 2
    I'd like you to know that you answered a year old question and a mere 7 hours later it solved my problem. :) Commented Feb 16, 2021 at 16:13
  • 2
    omg same, strangeeeeee. It must be due to a recent breaking change in webpack, although I don't remember upgrading it... Commented Feb 16, 2021 at 20:39
  • 2
    This is really strange!! I faced this issue out of no where and stuck on our prod build and finally could resolve it with upgrading the latest version!
    – SDB
    Commented Feb 17, 2021 at 1:17
  • 1
    I actually got this error when upgrading to v5 trying to solve another issue. Commented Mar 30, 2021 at 20:59
  • 1
    Upgrading to version 5 of the html-webpack-plugin worked!
    – Ronen
    Commented Jun 26, 2021 at 0:32
3

from html-webpack-plugin documentation

Don't set any loader

By default (if you don't specify any loader in any way) a fallback lodash loader kicks in.

{
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
}

Be aware, using .html as your template extention may unexpectedly trigger another loader.

please follow the documentation first and see whether you have any loader conflicts.

1
  • 3
    I am getting this error. I do not have loader conflicts. I have not set any loader for .html files.
    – trusktr
    Commented Feb 16, 2021 at 4:21

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