-
-
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
Storybook is not able to resolve path with Next js absolute import #11639
Comments
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
I have exactly the same problem! I tried adding a tsconfig.json and with a custom webpack but didn't work. |
+1 - Same issue here. Any idea on how to process Next.js paths in jsconfig.json? I found this on TypeScript (haven't tested it) but need one on Javascript instead: #9610 |
+1 Same problem here. NextJS resolves but SB don't. jsconfig.json
|
@kalanda That's correct, Next.js resolve but SB don't. I tried a lot of webpack solutions and multiple tsconfig.json options but anyone was successful. I decided to remove SB for my project for now. Sad. |
I havent gotten everything working with Next yet but this resolved the absolute import issues (in .storybook/webpack.config.js):
For using ./src, you can use path.resolve(__dirname, "..", "src"), |
With SB 6.x you don't have a webpack configuration, instead you use a main.js file with specific import structure (see Storybook React Configure Preview). Please, could you provide the same code in this format?
|
You can still add a webpack.config.js to your .storybook directory. But with main.js, you define it as webpackFinal:
|
Hello! I've just tried this fix in a project with Nextjs v9.5.3 and Storybook v6.0.21 but its not working. On the path /sb-test/stories/Button.stories.js on the the 2nd and 3rd lines you can switch between absolute and relative paths.
|
Its because thats not an absolute import from root, your path is using an alias. The following code allows you to import from src using "src/components/button/Button":
But it does not tell webpack anything about the "@/Components" syntax. You would need to pass an alias configuration like this:
|
Great! It worked perfectly, thanks a lot man. I wrote a commit implementing your fix: Absolute path with Storybook problem solved |
I love you man, I spent like about 40 hours trying to solve but I never could. Thanks <3
|
haha no problem, glad I could help 🍻 |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
thank you man you saved my day that's how it worked for next.js const path = require('path');
module.exports = {
stories: [
'../desktop/**/*.stories.@(ts|tsx)',
'../mobile/**/*.stories.@(ts|tsx)',
],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
webpackFinal: async (config, { configType }) => {
config.resolve.modules = [path.resolve(__dirname, '..'), 'node_modules'];
config.resolve.alias = {
...config.resolve.alias,
'@': path.resolve(__dirname, '../'),
};
return config;
},
}; |
Hi, I am going to close this for now, as there are 2 main alternatives:
|
now just install the next addon in your project. |
After some errors and look a way to work with NEXT components, this config works.
|
Anyone still having issues, I had two typescript configurations. Once you define the alias, and resolve the alias using |
Here is how I solved. I got the problem because I installed On the "aliases": {
- "components": "@/components",
- "utils": "@/lib/utils"
+ "components": "src/components",
+ "utils": "src/lib/utils"
} On the -"paths": {
- "@/*": ["./src/*"]
- }
+ "baseUrl": "."
After that, I went to the vscode search and replace tool (Ctrl + Shift + F); And I replaced all |
@valentinpalkovic @vanessayuenn This was raised on Twitter I've reproduced by editing the Storybook CLI template files: // stories/Header.tsx
import React from 'react';
import { Button } from '@/stories/Button';
|
I would love to work on it! |
@shilman @valentinpalkovic I don't know if it helps you or not, but I got it working by doing this: https://stackoverflow.com/a/68288720/7949258 In any case it might help someone having this problem (like me!) until you can fix it. |
I thought Storybook already used |
Actually, |
For my case, adding
|
This was fixed by #26651. The fix will be part of the 8.0.6 release! |
Describe the bug
Storybook is not able to resolve path with Next js absolute import
To Reproduce
Steps to reproduce the behavior:
yarn add storybook
Added .storybook folder
Add jsconfig for absolute import
Expected behavior
It should build correctly.
Error
Code snippets
I am using jsconfig.json
System:
The text was updated successfully, but these errors were encountered: