I can not import with absolute path in Storybook
See original GitHub issueVersion
- @storybook/addon-actions: 3.4.8
- @storybook/addon-links: 3.4.8
- @storybook/addon-storyshots: 3.4.8
- @storybook/addons: 3.4.8
- @storybook/react: 3.4.8
- React: 16.4.2
- TypeScript: 2.9.2
Behavior
An error will be displayed in storybook when creating the following components.
# src/components/organisms/ParentComponent/index.tsx
import * as React from 'react';
import ChildrenComponent from 'src/components/molecules/ChildrenComponent/index';
const ParentComponent: React.SFC<{}> = ({ ...props }) => (
<ChildrenComponent />
);
Error
Module not found: Error: Can't resolve 'src/components/molecules/ChildrenComponent/index' in '/AppRoot/src/components/organisms/ParentComponent'
This seems to be caused by loading with an absolute path, so if you do the following it works fine.
import ChildrenComponent from '../../molecules/ChildrenComponent/index';
However, I would like to avoid this method as much as possible. Is there no way to start storybook without errors other than specifying by relative path?
The application of the absolute path is based on the following description of tsconfig.json.
"compilerOptions": {
"outDir": "build/dist",
"rootDir": "src",
"baseUrl": "."
}
Also, this time we are importing a stories file written in tsx extension in compiled src directory rather than compiled build/dist directory on storybook this time.
This is set in ./storybook/config, but setting it tobuild/dist will produce similar results.
Since this project uses atomic design, it has roughly the following directory structure.
src
├── components
│ ├── molecules
│ │ ├── ChildrenComponent
│ │ │ ├── index.tsx
│ │ │ └── index.stories.tsx
│ ├── organisms
│ │ ├── ParentComponent
│ │ │ ├── index.tsx
│ │ │ └── index.stories.tsx
Related issues
There seemed to be something like the relevant issue. However, it did not reach a solution. #333 , #3438
Digression
As an aside, I believe there are two ways to solve this problem.
The first thing I would like to ask here is to “Import absolute path component with storybook”, the The second is to compile to a js file which imports the imported tsx file with an absolute path as a relative path. After that, apply the imported js file with the relative path to storybook.
Regarding the latter, I think it is not appropriate to listen to this problem here, but if you know it, I would like to ask about that method as well.
Thanks for your time.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:31 (9 by maintainers)
Top Related StackOverflow Question
In case anyone is looking at this for storybook 5,
Try adding something like this: