错误:找不到模块“ html-webpack-plugin”-Webpack(反应)

时间:2018-07-26 02:12:51

标签: javascript reactjs webpack plugins html-webpack-plugin

在此link

之后,我尝试使用webpack 4创建一个基本的React应用

直到安装“ html-webpack-plugin”,我没有遇到任何错误。但是,一旦运行命令“ npm run start”,我将不断收到以下错误:

**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)**

我尝试通过以下两个线程来解决此问题:在全局和本地安装软件包,但这无济于事。

error: cannot find module html-webpack-plugin

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

请在下面查看我的代码:

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"]
}

3 个答案:

答案 0 :(得分:2)

使用此命令:

npm i --save-dev html-webpack-plugin

答案 1 :(得分:2)

尝试删除节点模块:rm -rf node_modules

下一步,重新安装所有开发人员依赖项npm install

最后,重新安装 html-webpack-plugin npm i html-webpack-plugin --save-dev

我还建议确保所有目录路径均正确,并且您位于实际的项目文件夹中。这种情况很少见,但可能会不时发生。

答案 2 :(得分:-1)

您还可以使用以下命令解决错误。

npm install html-webpack-plugin