storybook: I can not import with absolute path in Storybook
Version
- @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.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 31 (9 by maintainers)
In case anyone is looking at this for storybook 5,
Try adding something like this:
I had similar issue where my absolute import works in
start-storybookbut not when I build. I’ll just put it here for reference if someone wants some reference.I’m using
@srcfor absolute import and made it work by adding this line:For context, my project directory looks like this:
Hope it helps 😃
For me, it worked by adding
__dirnametopath.resolvein my webpack config, like this: (I’m working on a create-react-app + TypeScript setup):My file structure, adjust yours accordingly:
For storybook 6.0, You may do this in
main.jsfileI’m using
storybook@6.4.9withwebpack5and I’m getting errors when I try this:I got this issue after using CLI and I was able to resolve it by modifying my .storybook/main.js to:
.storybook/webpack.config.js
const path =require(‘path’);` // const webpack = require(‘webpack’); const genDefaultConfig = require(‘@storybook/react/dist/server/config/defaults/webpack.config.js’);module.exports = (baseConfig, env) => { const config = genDefaultConfig(baseConfig, env); config.module = { rules: [ { test: /.tsx$/, loaders: [“ts-loader”], include: path.resolve(__dirname, ‘…/app/xv/’) }, { test: /.scss$/, loaders: [ ‘style-loader’, ‘css-loader’, ‘sass-loader?includePaths[]=’ + encodeURIComponent(path.resolve(__dirname, ‘…/app/’)) ] }, { test: /.css$/, loader: ‘style-loader!css-loader’ }, { test: /.less$/, loader: ‘style-loader!css-loader!less-loader’ }, { test: /.js|.ts$/, exclude: [path.join(__dirname, ‘app/components’), /node_modules/], loader: ‘ng-annotate-loader’ }, { test: /.js$/, exclude: [path.join(__dirname, ‘app/components’), /node_modules/], loader: ‘babel-loader?presets[]=es2015&presets[]=stage-1&presets[]=react&cacheDirectory’ } ] };
}`
// code
`import * as React from ‘react’;
import { autobind } from ‘xv/util/decorators’;
import ‘…/styles/ActionIcon.scss’;`
// error
`ERROR in ./app/xv/ui/components/ActionIcon.tsx
Module not found: Error: Can’t resolve ‘xv/util/decorators’ in ‘/Users/lukasanderson/workspace/NeXgen-UI/app/xv/ui/components’
@ ./app/xv/ui/components/ActionIcon.tsx 31:0-46 @ ./.storybook/stories/actionIcons.js @ ./.storybook/config.js @ multi ./node_modules/@storybook/react/dist/server/config/polyfills.js ./node_modules/@storybook/react/dist/server/config/globals.js (webpack)-hot-middleware/client.js?reload=true ./.storybook/config.js`
with /xv/ being mapped to the base of the project (root/app/xv/*) in the usual webpack dev (non storybook) build
sorry for the formatting
I’ve tried different variations of the regex replacement with NormalModuleReplacementPlugin, some found in similar threads/issues about storybook not handling absolute paths
example:
plugins: [ new webpack.NormalModuleReplacementPlugin(/xv/, function(resource) { resource.request = resource.request.replace(/xv/, '../../app/'); }) ]gives me this error:
`ERROR in ./.storybook/stories/actionIcons.js
Module not found: Error: Can’t resolve ‘…/…/app//ui/components/Tooltip’ in ‘/Users/lukasanderson/workspace/NeXgen-UI/.storybook/stories’
@ ./.storybook/stories/actionIcons.js 5:0-47 @ ./.storybook/config.js @ multi ./node_modules/@storybook/react/dist/server/config/polyfills.js ./node_modules/@storybook/react/dist/server/config/globals.js (webpack)-hot-middleware/client.js?reload=true ./.storybook/config.js `
and this change (adding /xv/ to the replacement string)
plugins: [ new webpack.NormalModuleReplacementPlugin(/xv/, function(resource) { resource.request = resource.request.replace(/xv/, '../../app/xv/'); }) ]gives
`ERROR in ./.storybook/stories/actionIcons.js
Module not found: Error: Can’t resolve ‘…/…/app/xv//ui/components/Tooltip’ in ‘/Users/lukasanderson/workspace/NeXgen-UI/.storybook/stories’
@ ./.storybook/stories/actionIcons.js 5:0-47 `
when I have the correct path replacement for absolute paths, not only are the relative paths messed up, but the path is ‘correct’ and an error persists:
ERROR in ./.storybook/stories/actionIcons.js Module not found: Error: Can't resolve '../../app/xv/ui/components/Tooltip' in '/Users/lukasanderson/workspace/NeXgen-UI/.storybook/stories'The stupid thing I failed to realise is I forgot to add a leading
../to myresolve.modulesconfig.Here’s what works for me:
This is because my storybook webpack config is located 1 directory deeper in the default
.storybookdir.You beautiful person @kexinlu1121. It worked perfectly, thank you.
Looks like you need to add
config.resolve.extensions.push('.ts', '.tsx');as wellSorry, this is a fairly late reply. I have the same set up as you an I got his working by changing this:
To this:
Hopefully that helps (someone)!
Hello I’m using React/TS I got the same issue my .storybook/main.js:
my structure
A ParentComponent component
The problem come from
src/componentsbecause is I dosrc/components/ChildrenComponentit works.I don’t get it I tried everything here and #333 #3291 and many others
A screen shot of the error
Can anyone help ?
@wzulfikar Thank your for your solution!
@glocore’s solution was the hint I needed for it to work. Thanks!
So having this
import { autobind } from 'xv/util/decorators';you should probably addpath.resolve('./app')to theresolve.modules