TypeError: Cannot read property 'tap' of undefined

See original GitHub issue

Current behaviour 💣

info @storybook/react v6.1.21
info
info => Loading presets
info => Loading presets
info => Adding stories defined in ".storybook\main.js"
info => Using default Webpack setup
ERR! TypeError: Cannot read property 'tap' of undefined
ERR!     at HtmlWebpackPlugin.apply (.\node_modules\html-webpack-plugin\index.js:41:31)
ERR!     at webpack (.\node_modules\@storybook\core\node_modules\webpack\lib\webpack.js:51:13)
ERR!     at startPreview (.\node_modules\@storybook\core\dist\server\dev-server.js:420:39)
ERR!     at async Promise.all (index 0)
ERR!     at async storybookDevServer (.\node_modules\@storybook\core\dist\server\dev-server.js:505:29)
ERR!     at async buildDevStandalone (.\node_modules\@storybook\core\dist\server\build-dev.js:317:33)
ERR!     at async buildDev (.\node_modules\@storybook\core\dist\server\build-dev.js:380:3)
ERR!  TypeError: Cannot read property 'tap' of undefined
ERR!     at HtmlWebpackPlugin.apply (.\node_modules\html-webpack-plugin\index.js:41:31)
ERR!     at webpack (.\node_modules\@storybook\core\node_modules\webpack\lib\webpack.js:51:13)
ERR!     at startPreview (.\node_modules\@storybook\core\dist\server\dev-server.js:420:39)
ERR!     at async Promise.all (index 0)
ERR!     at async storybookDevServer (.\node_modules\@storybook\core\dist\server\dev-server.js:505:29)
ERR!     at async buildDevStandalone (.\node_modules\@storybook\core\dist\server\build-dev.js:317:33)
ERR!     at async buildDev .\node_modules\@storybook\core\dist\server\build-dev.js:380:3)

Expected behaviour ☀️

Html-webpack-plugin will be compatible with storybook

Reproduction Example 👾

Packages:

- "@storybook/react": "^6.1.21",
-  "babel-loader": "^8.1.0",
-  "html-webpack-plugin": "^5.2.0",
-  "mini-css-extract-plugin": "^1.3.9",
-  "webpack": "^5.24.3",
-  "webpack-cli": "^4.2.0",
-  "webpack-dev-server": "^3.11.0"

Content of .storybook/main.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    stories: ['../src/Examples.stories.tsx'],
    webpackFinal: (config) => {
        return { ...config,
        plugins: [
          new MiniCssExtractPlugin(),
          new HtmlWebpackPlugin(),
        ],
        module: { 
            rules: [
                {
                  test: /\.(ts|js)x?$/,
                  exclude: /node_modules/,
                  use: {
                    loader: 'babel-loader',
                    options: require('../../babel.config.js')
                  }
                },
                {
                  test: /\.css$/i,
                  use: [MiniCssExtractPlugin.loader, 'css-loader'],
                },
                {
                  test: /\.(png|svg|gif)$/,
                  loader: 'file-loader',
                  options: {
                    outputPath: 'images',
                    name: '[name].[ext]'
                  }
                },
                {
                  test: /\.(woff|woff2|eot|ttf|otf)$/,
                  loader: 'file-loader',
                  options: {
                    outputPath: 'fonts',
                    name: '[name].[ext]'
                  }
                },
              ]
         } };
    },
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
AviVahlcommented, Mar 9, 2021

TypeError: Cannot read property 'tap' of undefined probably happens due to mixing of incompatible html-webpack-plugin and webpack versions.

TypeError: The 'compilation' argument must be an instance of Compilation probably happens due to having duplicate webpack copies in node_modules (either of the same or different versions) mixing together in a single compilation.

Check your npm/yarn/pnpm install log. Do you see any peerDependency warnings? These are the first that require fixing. Then make sure you don’t have duplicate webpack versions. You can do that by examining the lock files or node_modules itself. npm list or yarn list might come in handy.

2reactions
hg-pyuncommented, Mar 5, 2021

#1618

In my case, ts-loader occurs the issue.

Module build failed (from ./node_modules/ts-loader/index.js):
TypeError: Cannot read property 'tap' of undefined

webpack.config.js

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

const srcPath = path.resolve(__dirname, './src');
const distPath = path.resolve(__dirname, './dist');

module.exports = {
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  entry: ['react-hot-loader/patch', path.resolve(srcPath, './index.tsx')],
  output: {
    path: distPath,
    publicPath: '/',
    filename: 'bundle.[hash].js',
  },
  resolve: {
    extensions: ['.js', '.ts', '.json', '.tsx', '.jsx'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
      'react-dom': '@hot-loader/react-dom',
    },
  },
  module: {
    rules: [
      {
        test: /\.s?css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              modules: {
                localIdentName: '[local]',
              },
            },
          },
          'resolve-url-loader',
          {
            loader: 'sass-loader',
            options: {
              additionalData: '@import "src/assets/scss/lib.scss";',
            },
          },
        ],
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.(woff|woff2|png|jpe?g|gif|svg)/,
        use: [
          {
            loader: 'url-loader',
          },
        ],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html',
      filename: 'index.html',
    }),
  ],
};

Not work

const webpackConfig = require('../webpack.config.js');

module.exports = {
  stories: ['../src/components/**/*.stories.mdx', '../src/components/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
  typescript: {
    check: false,
    checkOptions: {},
    reactDocgen: 'react-docgen-typescript',
    reactDocgenTypescriptOptions: {
      shouldExtractLiteralValuesFromEnum: true,
      propFilter: (prop) => (prop.parent ? !/node_modules/.test(prop.parent.fileName) : true),
    },
  },
  webpackFinal: (config) => {
    return {
      ...config,
      module: {
        ...config.module,
        rules: webpackConfig.module.rules,
      },
      resolve: {
        ...config.resolve,
        ...webpackConfig.resolve,
        alias: {
          ...config.resolve.alias,
          ...webpackConfig.resolve.alias,
        },
      },
    };
  },
};

Work

const webpackConfig = require('../webpack.config.js');

module.exports = {
  stories: ['../src/components/**/*.stories.mdx', '../src/components/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
  typescript: {
    check: false,
    checkOptions: {},
    reactDocgen: 'react-docgen-typescript',
    reactDocgenTypescriptOptions: {
      shouldExtractLiteralValuesFromEnum: true,
      propFilter: (prop) => (prop.parent ? !/node_modules/.test(prop.parent.fileName) : true),
    },
  },
  webpackFinal: (config) => {
    config.module.rules.push({
      test: /\.s?css$/,
      use: [
        'style-loader',
        {
          loader: 'css-loader',
          options: {
            modules: {
              localIdentName: '[local]',
            },
          },
        },
        'resolve-url-loader',
        {
          loader: 'sass-loader',
          options: {
            additionalData: '@import "src/assets/scss/lib.scss";',
          },
        },
      ],
    })

    return {
      ...config,
      resolve: {
        ...config.resolve,
        ...webpackConfig.resolve,
        alias: {
          ...config.resolve.alias,
          ...webpackConfig.resolve.alias,
        },
      },
    };
  },
};

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property 'tap' of undefined #5781
I need to install webpack for react-styleguidist, but it seems if the version doesn't match the one Next is using this error occurs....
Read more >
ProgressPlugin TypeError: Cannot read property 'tap' of ...
In my case the error was that I put a plugin under resolve.plugins , instead of directly in plugins inside the webpack config....
Read more >
Cannot read property 'tap' of undefined in ts-loader #14157
Describe the bug. Module build failed (from ./node_modules/ts-loader/index.js): TypeError: Cannot read property 'tap' of undefined. To Reproduce ...
Read more >
Cannot read property 'tap' of undefined at HtmlWebpackPlugin ...
【Webpack】TypeError: Cannot read property 'tap' of undefined at HtmlWebpackPlugin.
Read more >
Webpack: Cannot read property 'tap' of undefined-Reactjs
[Solved]-React- Webpack: Cannot read property 'tap' of undefined-Reactjs ... I had the same exact problem. You have to add @babel/preset-react library to your ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found