簡體   English   中英

npm 運行 html-webpack-plugin 的構建錯誤

[英]npm run build error for html-webpack-plugin

嘿,所以我是 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 4 有一些棄用更改,html-webpack-plugin 的作者沒有更改新要求的代碼,這就是為什么這個插件不能與新的 webpack 4 版本一起使用。

核心 webpack 團隊分叉了這個插件,直到作者更改了這個包contrib/html-webpack-plugin可用的初始代碼和他們的分叉

所以你應該安裝這個叉子而不是原來的叉子。

您可以通過簡單地從 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 發布公告

我通過安裝測試版的HTML Webpack Plugin解決了這個問題。

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

對於那些正在尋找這個問題的更新解決方案的人,模塊webpack-contrib/html-webpack-plugin不再存在。 有關詳細信息,您可以查看此處的討論 解決方案npm install html-webpack-plugin --save-dev有效。 如果這引發了 OP 問題中描述的運行時錯誤,您可以嘗試安裝特定版本的模塊npm i html-webpack-plugin@4.4.0 希望這對你有用,就像對我一樣。

第一的:

npm install -g yarn

然后:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM