-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I can not import with absolute path in Storybook #3916
Comments
I don't know how does it work for you in your regular app (like what is your setup ?), but if you will put your cwd (assuming -> |
My
|
Try adding something like this: const path = require('path');
// blah blah code
module.exports = (baseConfig, env) => {
// blah blah code
config.resolve.modules = [
...(config.resolve.modules || []),
path.resolve('./'),
];
} |
It worked properly!! |
@igor-dv I am having a similar issue, and have tried your suggestion (above), as well as the NormalModuleReplacementPlugin which modifies the resource.request path. Neither have worked Do you have any other suggestions? |
Can you please share your code examples / webpack.config.js ? |
.storybook/webpack.config.js
module.exports = (baseConfig, env) => {
}` // 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 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:
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 and this change (adding /xv/ to the replacement string)
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:
|
What is your working directory (cwd)? |
root/
|
I start storybook from NeXgen-UI (aka project root) |
So having this |
alright, so I now have this `
` and am getting the same: `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' |
Looks like you need to add |
@igor-dv I gave that a go as well, same error. Thanks for the help on this |
🤔 I think I need to see the reproduction, then. Do you have a public repo? |
For me, it worked by adding config.resolve.modules = [
...(config.resolve.modules || []),
path.resolve(__dirname, "../"),
path.resolve(__dirname, "../src")
]; My file structure, adjust yours accordingly:
|
The stupid thing I failed to realise is I forgot to add a leading Here's what works for me:
This is because my storybook webpack config is located 1 directory deeper in the default |
In case anyone is looking at this for storybook 5,
|
You beautiful person @kexinlu1121. It worked perfectly, thank you. |
@glocore's solution was the hint I needed for it to work. Thanks! |
I had similar issue where my absolute import works in I'm using
For context, my project directory looks like this:
Hope it helps :) |
@wzulfikar Thank your for your solution! |
I got this issue after using CLI and I was able to resolve it by modifying my .storybook/main.js to:
|
Hello
my structure
A ParentComponent component
The problem come from I don't get it I tried everything here and #333 #3291 and many others Can anyone help ? |
Sorry, 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)! |
For storybook 6.0, You may do this in const path = require("path");
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/preset-create-react-app",
],
// ************* Add this **********
webpackFinal: async (config) => {
config.resolve.modules = [
...(config.resolve.modules || []),
path.resolve(__dirname, "../src"),
];
return config;
},
}; |
@wzulfikar thanks a ton, bro |
|
Note: If you are using typescript with So if you have
|
I'm using
|
Version
Behavior
An error will be displayed in storybook when creating the following components.
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
.Also, this time we are importing a
stories
file written in tsx extension in compiledsrc
directory rather than compiledbuild/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.
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 importedtsx
file with an absolute path as a relative path. After that, apply the importedjs
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.
The text was updated successfully, but these errors were encountered: