写的htmlWebpackPlugin获取它的options和files打包一直出错

来源:3-2 自动化生成项目中的html页面(中)

carolcoral

2017-09-26 19:27

indexhtml:

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <title><%= htmlWebpackPlugin.options.title %></title>

    </head>

    <body>

        <%= htmlWebpackPlugin.options.date%>

        <% for( var key in htmlWebpackPlugin){%><%= key%><%= }%>

        <% for( var key in htmlWebpackPlugin.files){%><%= key%>:<%= JSON.stringify(htmlWebpackPlugin.files[key])%><%= }%>

    </body>



打包时候一直提示“ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined ”



打包后的index.html文件内容:

Html Webpack Plugin:

<pre>

  TypeError: Cannot read property 'files' of undefined

  

  - index.html:91 

    /Users/carol/www/webpackDemo/index.html:91:43

  

  - index.html:104 module.exports

    /Users/carol/www/webpackDemo/index.html:104:3

  

  - index.js:264 

    [webpackDemo]/[html-webpack-plugin]/index.js:264:16

  

  - util.js:16 tryCatcher

    [webpackDemo]/[bluebird]/js/release/util.js:16:23

  

  - promise.js:512 Promise._settlePromiseFromHandler

    [webpackDemo]/[bluebird]/js/release/promise.js:512:31

  

  - promise.js:569 Promise._settlePromise

    [webpackDemo]/[bluebird]/js/release/promise.js:569:18

  

  - promise.js:606 Promise._settlePromiseCtx

    [webpackDemo]/[bluebird]/js/release/promise.js:606:10

  

  - async.js:138 Async._drainQueue

    [webpackDemo]/[bluebird]/js/release/async.js:138:12

  

  - async.js:143 Async._drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:143:10

  

  - async.js:17 Immediate.Async.drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:17:14

  

</pre>

在terminal中出现的错误提示:

Hash: f31b61d7e4a63d32cf9d

Version: webpack 3.6.0

Time: 590ms

                             Asset     Size  Chunks             Chunk Names

./js/main--aea7fcede5dd435d3e28.js  2.51 kB       0  [emitted]  main

   ./js/a--36facad12b17b00910bb.js  2.52 kB       1  [emitted]  a

                        index.html  1.02 kB          [emitted]  

   [0] ./src/script/main.js 21 bytes {0} [built]

   [1] ./src/script/a.js 22 bytes {1} [built]


ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined


ERROR in   TypeError: Cannot read property 'files' of undefined

  

  - index.html:91 

    /Users/carol/www/webpackDemo/index.html:91:43

  

  - index.html:104 module.exports

    /Users/carol/www/webpackDemo/index.html:104:3

  

  - index.js:264 

    [webpackDemo]/[html-webpack-plugin]/index.js:264:16

  

  - util.js:16 tryCatcher

    [webpackDemo]/[bluebird]/js/release/util.js:16:23

  

  - promise.js:512 Promise._settlePromiseFromHandler

    [webpackDemo]/[bluebird]/js/release/promise.js:512:31

  

  - promise.js:569 Promise._settlePromise

    [webpackDemo]/[bluebird]/js/release/promise.js:569:18

  

  - promise.js:606 Promise._settlePromiseCtx

    [webpackDemo]/[bluebird]/js/release/promise.js:606:10

  

  - async.js:138 Async._drainQueue

    [webpackDemo]/[bluebird]/js/release/async.js:138:12

  

  - async.js:143 Async._drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:143:10

  

  - async.js:17 Immediate.Async.drainQueues

    [webpackDemo]/[bluebird]/js/release/async.js:17:14

  


Child html-webpack-plugin for "index.html":

     1 asset

       [0] ./node_modules/html-webpack-plugin/lib/loader.js!./index.html 1.49 kB {0} [built]

       [2] (webpack)/buildin/global.js 509 bytes {0} [built]

       [3] (webpack)/buildin/module.js 517 bytes {0} [built]

        + 1 hidden module


写回答 关注

2回答

  • 猪小小屁
    2021-03-22 01:00:44

    <% for( var key in htmlWebpackPlugin){%><%= key%><%= }%>中最后的<%= }%>等号不要

  • carolcoral
    2017-09-26 19:44:26

    打包后的index.html文件内容:

    Html Webpack Plugin:

    <pre>

      TypeError: Cannot read property 'files' of undefined

      

      - index.html:91 

        /Users/carol/www/webpackDemo/index.html:91:43

      

      - index.html:104 module.exports

        /Users/carol/www/webpackDemo/index.html:104:3

      

      - index.js:264 

        [webpackDemo]/[html-webpack-plugin]/index.js:264:16

      

      - util.js:16 tryCatcher

        [webpackDemo]/[bluebird]/js/release/util.js:16:23

      

      - promise.js:512 Promise._settlePromiseFromHandler

        [webpackDemo]/[bluebird]/js/release/promise.js:512:31

      

      - promise.js:569 Promise._settlePromise

        [webpackDemo]/[bluebird]/js/release/promise.js:569:18

      

      - promise.js:606 Promise._settlePromiseCtx

        [webpackDemo]/[bluebird]/js/release/promise.js:606:10

      

      - async.js:138 Async._drainQueue

        [webpackDemo]/[bluebird]/js/release/async.js:138:12

      

      - async.js:143 Async._drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:143:10

      

      - async.js:17 Immediate.Async.drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:17:14

      

    </pre>

    在terminal中出现的错误提示:

    Hash: f31b61d7e4a63d32cf9d

    Version: webpack 3.6.0

    Time: 590ms

                                 Asset     Size  Chunks             Chunk Names

    ./js/main--aea7fcede5dd435d3e28.js  2.51 kB       0  [emitted]  main

       ./js/a--36facad12b17b00910bb.js  2.52 kB       1  [emitted]  a

                            index.html  1.02 kB          [emitted]  

       [0] ./src/script/main.js 21 bytes {0} [built]

       [1] ./src/script/a.js 22 bytes {1} [built]


    ERROR in Template execution failed: TypeError: Cannot read property 'files' of undefined


    ERROR in   TypeError: Cannot read property 'files' of undefined

      

      - index.html:91 

        /Users/carol/www/webpackDemo/index.html:91:43

      

      - index.html:104 module.exports

        /Users/carol/www/webpackDemo/index.html:104:3

      

      - index.js:264 

        [webpackDemo]/[html-webpack-plugin]/index.js:264:16

      

      - util.js:16 tryCatcher

        [webpackDemo]/[bluebird]/js/release/util.js:16:23

      

      - promise.js:512 Promise._settlePromiseFromHandler

        [webpackDemo]/[bluebird]/js/release/promise.js:512:31

      

      - promise.js:569 Promise._settlePromise

        [webpackDemo]/[bluebird]/js/release/promise.js:569:18

      

      - promise.js:606 Promise._settlePromiseCtx

        [webpackDemo]/[bluebird]/js/release/promise.js:606:10

      

      - async.js:138 Async._drainQueue

        [webpackDemo]/[bluebird]/js/release/async.js:138:12

      

      - async.js:143 Async._drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:143:10

      

      - async.js:17 Immediate.Async.drainQueues

        [webpackDemo]/[bluebird]/js/release/async.js:17:14

      


    Child html-webpack-plugin for "index.html":

         1 asset

           [0] ./node_modules/html-webpack-plugin/lib/loader.js!./index.html 1.49 kB {0} [built]

           [2] (webpack)/buildin/global.js 509 bytes {0} [built]

           [3] (webpack)/buildin/module.js 517 bytes {0} [built]

            + 1 hidden module


    w24584...

    跟你一样的问题,请问解决了吗?

    2017-11-23 18:05:49

    共 1 条回复 >

webpack深入与实战

webpack实战教程,用真实项目带你探索 webpack 强大的功能

86561 学习 · 721 问题

查看课程

相似问题