webpack 错误:找不到模块“txtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtxtx

7ajki6be  于 2023-04-06  发布在  Webpack
关注(0)|答案(6)|浏览(204)

我尝试使用webpack 4创建一个基本的React应用程序,如下所示link
直到安装“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

请看我下面的代码:
包.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"]
}
4smxwvx5

4smxwvx51#

使用此命令:

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

tgabmvqs2#

尝试删除节点模块:rm -rf node_modules
接下来重新安装所有的开发依赖npm install
最后,重新安装html-webpack-pluginnpm i html-webpack-plugin --save-dev
我还建议确保所有的目录路径都是正确的,并且你在实际的项目文件夹中。这是罕见的,但有时会发生。

0qx6xfy6

0qx6xfy63#

在我的例子中,使用typescript,sublime没有选择它。甚至不需要安装@types/html-webpack-plugin,它已经存在。只需要重新启动IDE让它注册。

bbmckpt7

bbmckpt74#

我尝试了一个替换版本来解决这个问题
npm install html-webpack-plugin@4.5.0

1wnzp6jl

1wnzp6jl5#

在我的情况下,我解决了一个问题时,重命名

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

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

thtygnil6#

您也可以使用下面的命令来解决错误。

npm install html-webpack-plugin

相关问题