【问题标题】:npm run build error for html-webpack-pluginhtml-webpack-plugin 的 npm 运行构建错误
【发布时间】:2018-08-06 15:41:34
【问题描述】:

嘿,我是 webpack 的新手,我正在尝试将它用于使用 Cesium js api 的项目。我正在在线关注他们的教程到 T 但由于某种原因,当我运行命令“npm run build”时,我收到以下错误,但没有正确执行。

Peris-MacBook-Pro:App peri$ npm run build

> ngafid@1.0.0 build /Users/peri/Desktop/App
> webpack --config webpack.config.js

(node:23326) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
(node:23326) DeprecationWarning: Tapable.apply is deprecated. Call apply on the plugin directly instead
/Users/peri/Desktop/App/node_modules/html-webpack-plugin/lib/compiler.js:81
        var outputName = compilation.mainTemplate.applyPluginsWaterfall('asset-path', outputOptions.filename, {
                                                  ^

TypeError: compilation.mainTemplate.applyPluginsWaterfall is not a function
    at /Users/peri/Desktop/App/node_modules/html-webpack-plugin/lib/compiler.js:81:51
    at compile (/Users/peri/Desktop/App/node_modules/webpack/lib/Compiler.js:222:11)
    at hooks.afterCompile.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compiler.js:470:14)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at compilation.seal.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compiler.js:467:30)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeAssets.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:957:35)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeChunkAssets.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:948:32)
    at _err0 (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
    at /Users/peri/Desktop/App/node_modules/uglifyjs-webpack-plugin/dist/index.js:262:11
    at _class.runTasks (/Users/peri/Desktop/App/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:63:9)
    at UglifyJsPlugin.optimizeFn (/Users/peri/Desktop/App/node_modules/uglifyjs-webpack-plugin/dist/index.js:182:16)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:7:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.additionalAssets.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:943:36)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at hooks.optimizeTree.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:939:32)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook [as _callAsync] (/Users/peri/Desktop/App/node_modules/tapable/lib/Hook.js:35:21)
    at Compilation.seal (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:881:27)
    at hooks.make.callAsync.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compiler.js:464:17)
    at _err0 (eval at create (/Users/peri/Desktop/App/node_modules/tapable/lib/HookCodeFactory.js:24:12), <anonymous>:11:1)
    at _addModuleChain (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:749:12)
    at processModuleDependencies.err (/Users/peri/Desktop/App/node_modules/webpack/lib/Compilation.js:688:9)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ngafid@1.0.0 build: `webpack --config webpack.config.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the ngafid@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/peri/.npm/_logs/2018-02-27T04_17_33_336Z-debug.log

我的 wepack.config.js 文件中有这个:

const path = require('path');

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    context: __dirname,
    entry: {
        app: './src/index.js'
    },
    output: {
        filename: '[name].js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [{
            test: /\.css$/,
            use: [ 'style-loader', 'css-loader' ]
        }, {
            test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
            use: [ 'url-loader' ]
        }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        })
    ]
};

我在 package.json 中添加了以下脚本行

  "scripts": {
    "build": "node_modules/.bin/webpack --config webpack.config.js"
  }

我不确定自己做错了什么。我试图查看警告是否是问题所在,但似乎并非如此。我假设它是导致错误的那个功能,但我该如何解决呢?非常感谢!

【问题讨论】:

  • 我的第一个猜测是您使用的是不兼容的 Webpack 版本和 HtmlWebpackPlugin。您使用的是哪些版本? (请检查您的package-lock.json 以确保)。
  • 为了完整起见,请将您的 package.json 文件提取添加到问题中

标签: javascript node.js npm webpack cesium


【解决方案1】:

Webpack 4 有一些弃用更改,并且 html-webpack-plugin 的作者没有更改新要求的代码,这就是该插件不适用于新的 webpack 4 版本的原因。

核心 webpack 团队分叉了这个插件,直到作者更改了这个包可用的初始代码和他们的分叉contrib/html-webpack-plugin

所以你应该安装这个 fork 而不是原来的。

您只需从 npm 或 yarn 安装即可 例如

npm i -D html-webpack-plugin@webpack-contrib/html-webpack-plugin

或者用纱线

yarn add -D html-webpack-plugin@webpack-contrib/html-webpack-plugin

不需要完全使用 yarn =) 你也可以通过 npm 来实现

更多详细信息:Webpack 4 release announce

【讨论】:

  • webpack 团队在the release notes for v4 上写道:更新(2018 年 3 月 1 日):html-webpack-plugin@3 现已提供 v4 支持!!!!这意味着现在运行 npm i --save-dev html-webpack-plugin 就足够了
【解决方案2】:

我通过安装HTML Webpack Plugin 的测试版解决了这个问题。

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

【讨论】:

    【解决方案3】:

    对于那些正在寻找此问题的更新解决方案的人,模块 webpack-contrib/html-webpack-plugin 不再存在。有关更多详细信息,您可以在此处查看discussion。解决方案npm install html-webpack-plugin --save-dev 有效。如果这引发了 OP 问题中描述的运行时错误,您可以尝试安装特定于版本的模块 npm i html-webpack-plugin@4.4.0。希望这对你有用,就像对我一样。

    【讨论】:

    • npm i html-webpack-plugin@4.4.0 --save-dev 救了我,谢谢
    【解决方案4】:

    第一:

    npm install -g yarn

    然后:

    yarn add webpack-contrib/html-webpack-plugin -D

    【讨论】:

      猜你喜欢
      • 2018-06-29
      • 2022-11-02
      • 2020-10-07
      • 2018-05-05
      • 2021-12-24
      • 2021-03-22
      • 2017-09-28
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多