storybook: [Bug]: SB_BUILDER-WEBPACK5_0003 (WebpackCompilationError): There were problems when compiling your code with Webpack.

Describe the bug

running yarn storybook --debug-webpack does not build once I have imported ts files.

WARN storyStoreV6 is deprecated, please migrate to storyStoreV7 instead.
WARN - Refer to the migration guide at https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#storystorev6-and-storiesof-is-deprecated
info => Starting manager..
info => Starting preview..
WARN Expected '@storybook/addon-actions' (or '@storybook/addon-essentials') to be listed before '@storybook/addon-interactions' in main Storybook config.
info Using Babel compiler
info Addon-docs: using MDX2
info => Using implicit CSS loaders
info => [@storybook/addon-styling-webpack] Applying custom Storybook webpack configuration styling.
info => Using default Webpack5 setup
<i> [webpack-dev-middleware] wait until bundle finished
ERROR in ./node_modules/@amplio/core/core/components/Alert/index.tsx 3:7
Module parse failed: Unexpected token (3:7)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| export { Alert } from 'core/components/Alert/Alert';
| export { AlertType } from 'core/components/Alert/types';
> export type {
|   AlertContentProps,
|   AlertMessageProps,
 @ ./src/stories/Core/Components/Alert.stories.tsx 1:0-70 3:13-18 10:10-24
 @ ./src/ sync ^\.(?:(?:^%7C[\\/]%7C(?:(?:(?%21(?:^%7C[\\/])\.).)*?)[\\/])(?%21\.)(?=.)[^\\/]*?\.stories\.(js%7Cjsx%7Cmjs%7Cts%7Ctsx))$ ./stories/Core/Components/Alert.stories.tsx
 @ ./generated-stories-entry.cjs 3:123-259

To Reproduce

main.js


import type { StorybookConfig } from "@storybook/react-webpack5";

import { join, dirname } from "path";

/**
 * This function is used to resolve the absolute path of a package.
 * It is needed in projects that use Yarn PnP or are set up within a monorepo.
 */
function getAbsolutePath(value: string): any {
  return dirname(require.resolve(join(value, "package.json")));
}
const config: StorybookConfig = {
  stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"],
  addons: [
    getAbsolutePath("@storybook/addon-links"),
    getAbsolutePath("@storybook/addon-essentials"),
    getAbsolutePath("@storybook/addon-onboarding"),
    getAbsolutePath("@storybook/addon-interactions"),
    getAbsolutePath("storybook-css-modules-preset"),
    getAbsolutePath("@storybook/addon-themes"),
    getAbsolutePath("@storybook/addon-viewport"),
    getAbsolutePath("@storybook/addon-controls"),
    getAbsolutePath("@storybook/addon-outline"),

    getAbsolutePath("@storybook/addon-styling-webpack"),
  ],
  framework: {
    name: getAbsolutePath("@storybook/react-webpack5"),
    options: {},
  },
  docs: {
    autodocs: "tag",
  },
  features: {
    storyStoreV7: false,
  },
  core: {
    builder: {
      name: '@storybook/builder-webpack5',
      options: {
        fsCache: true,
        lazyCompilation: true,
      },
    },
  },
};
export default config;

story


import React from 'react';
import { Meta, StoryObj } from '@storybook/react';
import { Alert, AlertType } from '@amplio/core/core/components/Alert';

const meta: Meta<typeof Alert> = {
    component: Alert,
    title: 'Alert',
};

export default meta;
type Story = StoryObj<typeof Alert>;

export const FirstStory: Story = {
    args: {
        //👇 The args you need here will depend on your component
        type: AlertType.INFO, // Adjust as needed
    },
};

package.json

{
  "name": "@amplio/storybook",
  "version": "2024.3.1-1045-DEV",
  "author": "xyz",
  "license": "yzx",
  "repository": {
    "type": "git",
    "url": "xyz"
  },
  "resolutions": {
    "coa": "2.0.2",
    "@types/react": "^18.0.21",
    "@types/history": "4.7.11"
  },
  "peerDependencies": {
    "react-redux": "7.2.6"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.24.0",
    "@babel/preset-react": "^7.23.3",
    "@babel/preset-typescript": "^7.23.3",
    "@reduxjs/toolkit": "1.9.5",
    "@storybook/addon-essentials": "7.6.17",
    "@storybook/addon-interactions": "7.6.17",
    "@storybook/addon-links": "7.6.17",
    "@storybook/addon-onboarding": "1.0.11",
    "@storybook/blocks": "7.6.17",
    "@storybook/react": "7.6.17",
    "@storybook/react-webpack5": "7.6.17",
    "@storybook/testing-library": "0.2.2",
    "@tinymce/tinymce-react": "3.10.4",
    "@types/date-fns": "2.6.0",
    "@types/history": "4.7.11",
    "@types/node": "16.6.1",
    "@types/react": "18.0.21",
    "@types/react-outside-click-handler": "^1.3.3",
    "@types/react-redux": "7.1.7",
    "@types/react-router-dom": "5.3.3",
    "@types/react-select": "5.0.1",
    "@types/styled-components": "5.1.9",
    "@types/yup": "0.29.13",
    "@typescript-eslint/eslint-plugin": "6.3.0",
    "@typescript-eslint/parser": "6.3.0",
    "babel-plugin-styled-components": "2.1.4",
    "case-sensitive-paths-webpack-plugin": "2.4.0",
    "eslint": "8.12.0",
    "eslint-config-airbnb-typescript": "17.1.0",
    "eslint-config-prettier": "8.5.0",
    "eslint-import-resolver-typescript": "3.5.5",
    "eslint-import-resolver-webpack": "0.13.4",
    "eslint-plugin-import": "2.25.4",
    "eslint-plugin-prettier": "4.0.0",
    "eslint-plugin-react": "7.29.4",
    "eslint-plugin-react-hooks": "4.4.0",
    "eslint-plugin-storybook": "^0.7.0",
    "focus-visible": "5.2.0",
    "formik": "2.2.9",
    "history": "4.10.1",
    "object-to-formdata": "4.4.1",
    "prettier": "2.0.1",
    "qs": "6.10.1",
    "react-bootstrap": "1.6.3",
    "react-bootstrap-table-next": "4.0.3",
    "react-bootstrap-table2-paginator": "2.1.2",
    "react-bootstrap-table2-toolkit": "2.1.3",
    "react-bootstrap-typeahead": "5.1.4",
    "react-cookie": "4.0.3",
    "react-datepicker": "3.1.3",
    "react-dom": "^18.2.0",
    "react-dropzone": "11.3.0",
    "react-idle-timer": "4.6.3",
    "react-number-format": "4.7.3",
    "react-outside-click-handler": "1.3.0",
    "react-redux": "7.2.6",
    "react-router-bootstrap": "0.25.0",
    "react-router-dom": "5.3.4",
    "react-scroll": "1.8.1",
    "react-select": "5.1.0",
    "styled-components": "4.3.2",
    "stylelint-config-styled-components": "0.1.1",
    "stylelint-processor-styled-components": "1.10.0",
    "tinymce": "5.7.0",
    "typesafe-actions": "5.1.0",
    "typescript": "5.1.6",
    "typescript-plugin-styled-components": "3.0.0",
    "use-debounce": "7.0.0",
    "uuid": "8.3.0",
    "yup": "0.31.1"
  },
  "private": true,
  "dependencies": {
    "@amplio/core": "2024.3.1-1045-DEV",
    "@storybook/addon-actions": "^7.6.17",
    "@storybook/addon-styling-webpack": "^0.0.6",
    "@storybook/addon-themes": "^7.6.17",
    "@storybook/test": "^7.6.17",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "@types/jest": "^27.5.2",
    "@types/node": "^16.18.87",
    "@types/react": "^18.2.64",
    "@types/react-dom": "^18.2.21",
    "case-sensitive-paths-webpack-plugin": "2.4.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "storybook": "^7.6.17",
    "storybook-css-modules-preset": "^1.1.1",
    "ts-loader": "^9.5.1",
    "typescript": "^4.9.5",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "npx webpack --config .storybook/webpack.config.ts",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "storybook": "storybook dev -p 6006",
    "build-storybook": "storybook build"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest",
      "plugin:storybook/recommended"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

System

No response

Additional context

This error only occurs once trying to import files.

I have tried using a webconfig but this also had the same issues.

We have no issue using this import method in any other implementations of our components, leading me to believe its a storybook issue

module.exports = {
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },

    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
};

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 26 (8 by maintainers)

Most upvoted comments

I made a version of storybook 7 and solved it

I encountered same issue. It is worth noting that babel has been moved out as part of major v8 upgrade, and you will need to set up by yourself.

For me, install @storybook/addon-webpack5-compiler-babel and @babel/preset-env, @babel/preset-react, setup .babelrc.js file, then the issue gone.

@harsharavindra Hey, what do you mean by “adding the webpack.config.js”? Where did you add it?

I just added webpack.config.js inside .storybook folder. Our project was having v6 storybook and tried to update it. I was trying to setup the framework @storybook/react-webpack5 and I have attached the folder structure in the below screenshot. Followed these steps.

Firstly, I ran npx storybook@latest upgrade and When I tried to run storybook, ended up with above error.

Following might be the cause:

  • inside main.js, make sure your stories are pointing to correct story files. This is how my main.js looks like
module.exports = {
  stories: [], // add your story path here. 
  addons: ['@storybook/addon-essentials', '@storybook/addon-a11y', '@storybook/addon-designs'],
  framework: {
    name: '@storybook/react-webpack5',
    options: {}
  }
};
  • this is how my webpack.config looks like
const path = require('path');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = async ({config}) => {
  // Ensure plugins array exists
  if (!config.plugins) {
    config.plugins = [];
  }

  // Add Babel loader for .js, .jsx, .ts, and .tsx files
  config.module.rules.push({
    test: /\.(js|jsx|ts|tsx)$/,
    exclude: /node_modules/,
    use: {
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env', '@babel/preset-react', '@babel/preset-typescript']
      }
    }
  });

  // Ensure the resolve.extensions includes .ts and .tsx
  config.resolve.extensions.push('.ts', '.tsx');

  // Add ForkTsCheckerWebpackPlugin to the plugins
  config.plugins.push(
    new ForkTsCheckerWebpackPlugin({
      async: false, // Optional: Set to false to run checks synchronously
      typescript: {
        configFile: path.resolve(__dirname, '../tsconfig.json') // Ensure the correct path to your tsconfig.json
      }
    })
  );

  return config;
};

I tried to follow the docs on official site, but was finding hard to resolve these errors. Then, I just used chatgpt. Enter all the technical details and it should provide you complete steps. if you get any error, just ask chatgpt 😅

I wish I can post our github, but cant make it public. I will try to spin up codesandbox and post it here later this week.

Image