Error: Cannot find module 'html-webpack-plugin' - Webpack (React)
Asked Answered
I

6

20

I tried to create a basic React app with webpack 4 following this link

until installing "html-webpack-plugin", I did not face any errors. But, once I run the command "npm run start", I keep getting the following error:

**Error: Cannot find module 'html-webpack-plugin'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)**

I tried to solve this issue using the following two threads by installing packages globally and locally, but it didn't help.

error: cannot find module html-webpack-plugin

https://github.com/webpack/webpack-dev-server/issues/1330

Please see my code below:

package.json:

{
  "name": "react_website",
  "version": "1.0.0",
  "description": "Website using React and Webpack",
  "main": "index.js",
  "scripts": {
    "start": "webpack --mode development",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "webpack": "^4.16.2",
    "webpack-cli": "^2.1.5"
  },
  "dependencies": {
    "react": "^16.4.1",
    "react-dom": "^16.4.1"
  }
}

webpack.config.js:

const HtmlWebPackPlugin = require("html-webpack-plugin");

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  },
  plugins: [
    new HtmlWebPackPlugin({
    template: "./src/index.html",
    filename: "./index.html"
  })
],
};

- .babelrc:

{
  "presets": ["env", "react"]
}
Impressionable answered 26/7, 2018 at 2:12 Comment(8)
What I would suggest is , better delete your complete node modules and then try npm installPitiful
I did that, but it did not work!!Impressionable
Can you run npm cache clean -f. And check it. It will basically clears the package cachePitiful
UPDATE: I noticed that I cannot see the version for "html-webpack-plugin". It displays as it is not installed. However, I have installed it, and it is loaded in "package.json" folder.Impressionable
Check if it's there in node_modules folder.Pitiful
I found that there isn't any folder based on "html-webpack-plugin"Impressionable
Yeah, so your package is not getting installed . Please check the log on terminal. If there are any permission issue. Most of the time it's the case. Try install html-webpack-plugin seperately. npm install html-webpack-plugin --save-dev. If not happening try with sudo or with administrative priviledge if you are on windowsPitiful
rm -rf node_modules npm install npm i html-webpack-plugin --save-dev Try this way.Gaven
B
17

Use this command:

npm i --save-dev html-webpack-plugin
Bisson answered 22/3, 2019 at 21:55 Comment(1)
I have html-webpack-plugin in package.json like OP, but webpack isn't detecting it. I also deleted my node modules and tried the whole thing again. Still nothing. Is there any way to get more info on where webpack is looking?Pipistrelle
S
3

Try removing Node Modules: rm -rf node_modules.

Next re-install all dev dependencies npm install.

Lastly, re-install the html-webpack-plugin npm i html-webpack-plugin --save-dev.

I would also suggest making sure all of the directory paths are correct and that you're inside the actual project folder. This is rare, but can happen from time to time.

Sulphurous answered 4/9, 2020 at 15:19 Comment(0)
O
0

In my case, using typescript, sublime didn't pick it up. Don't even need to install @types/html-webpack-plugin, it already exists. Just needed to restart the IDE for it to register.

Opossum answered 8/11, 2021 at 12:41 Comment(0)
D
0

I tried a replacement version to fix the problem

npm install [email protected]

Dill answered 18/4, 2022 at 5:27 Comment(0)
S
0

In my case I fixed a problem by renaming

const HTMLWebPackPlugin = require("html-webpack-plugin");

to

const HtmlWebPackPlugin = require("html-webpack-plugin");
Strawflower answered 31/3, 2023 at 15:51 Comment(0)
O
-3

You can also resolve error by using below command.

npm install html-webpack-plugin
Overalls answered 25/4, 2020 at 17:26 Comment(1)
This does not add to the existing answer. In fact, it is worse than the existing answer since you're installing it as a dependency instead of a dev dependency.Rammer

© 2022 - 2024 — McMap. All rights reserved.