Storybook: Can't resolve components path that are imported using the directory and default index.js file

Created on 6 Dec 2018  Β·  2Comments  Β·  Source: storybookjs/storybook

Describe the bug
Storybook can't resolve components path that are imported from a directory with the default index.js

β”œβ”€β”€ .storybook
β”‚   β”œβ”€β”€ config.js
β”‚   β”œβ”€β”€ addons.js
β”‚   β”œβ”€β”€ webpack.config.js
β”œβ”€β”€ app
β”‚   β”œβ”€β”€ javascript
β”‚   β”‚   β”œβ”€β”€ src
β”‚   β”‚   β”‚   β”œβ”€β”€ form
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ checkbox.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ radio.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ form.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ form.stories.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.js
β”‚   β”‚   β”‚   β”œβ”€β”€ alert
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ alertstuff.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ alert.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€utils
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ utilsstuff.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ utilsotherstuff.jsx
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ index.jsx

form/index.js contains:

import Form from './form.jsx';
import Radio from './radio.jsx';
import Checkbox from './checkbox.jsx';

export { Checkbox, Radio, Form };

alert/index.js is the same:

import AlertStuff from './alertstuff.jsx';
import Alert from './alert.jsx';

export { AlertStuff, Alert };

utils/index.js contains:

import UtilsStuff from './utilsstuff.jsx';
import UtilsOtherStuff './utilsotherstuff.jsx';

export { UtilsStuff, UtilsOtherStuff };

form.jsx contiains:

import { AlertStuff } from 'alert';

export class Form extends Component {
    ....
}

form/form.stories.jsx looks like this:

import { storiesOf } from '@storybook/react';
import React from 'react';
import Form from './form';


storiesOf('Form', module)
    .addWithJSX('simple', () => (
        <Form 
            name={text('Name', 'Form1')}
        />
    ));

.storybook/webpack.config.js looks like this:

const path = require("path");

// Export a function. Accept the base config as the only param.
module.exports = (storybookBaseConfig, configType) => {

  storybookBaseConfig.module.rules.push({
    test: /\.sass$/,
    loaders: ["style-loader", "css-loader", "sass-loader"],
    include: path.resolve(__dirname, "../")
  });

  return storybookBaseConfig;
};

.storybook/config.js:

import { withKnobs } from '@storybook/addon-knobs/react';
import { addDecorator, configure, setAddon } from '@storybook/react';
import JSXAddon from 'storybook-addon-jsx';

addDecorator(withKnobs);
window.regeneratorRuntime = require('babel-runtime/regenerator');
setAddon(JSXAddon);
const req = require.context('../app/javascript/src', true, /.stories.jsx$/);

function loadStories() {
  require('./welcomeStory');
  req.keys().forEach(file => req(file));
}

configure(loadStories, module);

When running npm run storybook I get the following error:

ERROR in ./app/javascript/src/form/form.jsx
Module not found: Error: Can't resolve 'alert' in '/Users/xxx/Code/xxx/xxx/app/javascript/src/form'

These components load and work fine

Storybook should be able to resolve the path when Storybook uses the component

System:

  • OS: [MacOS]
  • Device: [Macbook Pro 2018]
  • Browser: chrome,
  • Framework: React
  • Version: 4.0.11
babel / webpack question / support

Most helpful comment

Solved it with the following in the webpack.config.js file:

  storybookBaseConfig.resolve.modules = [
    ...(storybookBaseConfig.resolve.modules || []),
    path.resolve(__dirname, "../app/javascript/src"),
  ];

All 2 comments

Solved it with the following in the webpack.config.js file:

  storybookBaseConfig.resolve.modules = [
    ...(storybookBaseConfig.resolve.modules || []),
    path.resolve(__dirname, "../app/javascript/src"),
  ];

Resolved the issue with npm i @storybook/react

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zvictor picture zvictor  Β·  3Comments

sakulstra picture sakulstra  Β·  3Comments

rpersaud picture rpersaud  Β·  3Comments

levithomason picture levithomason  Β·  3Comments

xogeny picture xogeny  Β·  3Comments