storybook: Cant add aliases in storybook config
Cant add aliases to storybook 6^ main.js
const path = require("path");
module.exports = {
webpackFinal: async (config) => {
config.resolve.alias = {
...config.resolve.alias,
"~/": path.resolve(__dirname, "../src/"),
};
config.resolve.extensions.push(".ts", ".tsx");
return config;
},
};
{
"compilerOptions": {
"allowJs": false,
"allowSyntheticDefaultImports": true,
"baseUrl": ".",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "react",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"rootDirs": ["src", "stories"],
"outDir": "./dist",
"strict": true,
"skipLibCheck": true,
"target": "es6",
"typeRoots": ["./node_modules/@types"],
"paths": {
"~/": ["./src/"]
}
},
"exclude": ["./node_modules", "./dist"],
"include": ["src", "stories"]
}
error
Module not found: Error: Can't resolve '~/' in '/Users/user/dev/storybook-starter/stories'
@ ./stories/develop.stories.tsx 54:0-31 57:13-22 72:25-34
@ ./stories sync nonrecursive ^\.\/(?:(?!\.)(?=.)[^/]*?\.stories\.(ts|tsx))$
@ ./.storybook/generated-stories-entry.js
@ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/storybook-init-framework-entry.js ./node_modules/@storybook/addon-docs/dist/frameworks/common/config.js-generated-other-entry.js ./node_modules/@storybook/addon-docs/dist/frameworks/react/config.js-generated-other-entry.js ./node_modules/@storybook/addon-actions/dist/preset/addDecorator.js-generated-other-entry.js ./node_modules/@storybook/addon-actions/dist/preset/addArgs.js-generated-other-entry.js ./.storybook/preview.js-generated-config-entry.js ./.storybook/generated-stories-entry.js (webpack)-hot-middleware/client.js?reload=true&quiet=false&noInfo=undefined
files

What i am doing wrong?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 18
- Comments: 34 (9 by maintainers)
Commits related to this issue
- fix(core-components): @nrwl/storybook@13.10.6 build This fixes miscellaneous build bugs, but not a major one. But major bug remains: storybook does not recognize aliases. Known Issue: unable to re... — committed to TACC/tup-ui by wesleyboar 2 years ago
- fix(core-components): @nrwl/storybook@13.10.6 build #2 Major bug—storybook does not recognize aliases—workaround for CSS files: - install some utilities - rename postcss yaml config as a base refere... — committed to TACC/tup-ui by wesleyboar 2 years ago
I think the problem may be trailing slashes. Try using
~and../src/exactly. It seems to work for me in all the variants below (Storybook 6.0.21 and 6.0.27).Shortest:
Short:
Longer:
If someone could create a reproduction repo, that’d be super helpful fixing this bug.
if this doesn’t work
then that’s a bug.
This works properly for me on the latest version:
Here is a workaround for using aliases from tsconfig (*.mdx stories are supported as well):
I guess this should be added to Storybook docs.
I know this is an old ticket with numerous “try this” things listed in it, but fundamentally, the issue is reproducible by doing this:
npm init vue@latestSelect:
Now install storybook
Now create a basic component:
and a basic story:
and run:
npm run storybookYou will see:
…but why?
Well, bluntly because the storybook configuration is wrong.
The automatically generated vite config for storybook and the vite config from the vue scaffold are different; the scaffold looks like this:
and tsconfig.json that says:
and
.storybook/main.jsdoesn’t load that file. Or doesn’t process it. Or something.If you modify main.js to have this import, it works, eg:
So is this an issue or not?
Well, lets put it this way.
Anyone who uses the official (https://vuejs.org/guide/quick-start.html#with-build-tools) vue scaffold, with the official storybook install (https://storybook.js.org/docs/vue/get-started/install), will get an ‘out of the box’ copy of storybook that is broken.
I don’t know why this issue is closed with ‘completed’ because a work around exists, but, for anyone who finds this post, no, it’s not just you, it’s broken.
If you feel strongly about it, consider opening a new issue, a PR…
…but please appreciate that fundamentally the reason this is broken is because the alias is defined in the root configuration files, and those files are not used by storybook; the version generated by storybook does not include them; if you want to fix it, you need to look at what your configuration actually does and back port that into the storybook config, not just randomly copy and paste changes from this thread.
this was working fine for me with Storybook 5.x, but is now broken in 6.x
the exact scenario described by @ndelangen is not working for me, so i’m inclined to believe that this is a bug
i also still see
info => Using default Webpack setup.in the console, even though i am providing custom config like so:@monolithed posted a nice solution, but i wish this was cleaner with my prettier formating it looks like this
Apart from that for TypeScript users this is how i have it setup: in
tsconfig.tsand in
./.storybook/main.jsUpdate: looks like this is a cleaner version
I’m also running into this issue, having tried the solution here https://github.com/storybookjs/storybook/issues/11989#issuecomment-674833064.
@ndelangen Check out this repro repository – https://github.com/mattrothenberg/sb6-resolve-alias-error
You should be able to clone, run
yarn && yarn storybookand see that the tilde (“~”) alias doesn’t resolve for the Button component.For me, using NextJs, this is solved the problem:
I just came across this after having some issues having upgraded storybook to 6.
Turned out my problem was an incorrect resolve function for whatever reason.
My
main.jsis in.storybookand I am using this function:Example:
Otherwise the same setup described by @ndelangen above and it is working.
Without thinking too far, I think updating config object might not work as expected. A simple workaround for you @toastyboost
In addition with 0 config typescript, you no longer need
config.resolve.extensions.push(".ts", ".tsx");Do we have any update for this issue? I’m still facing this issue with latest Storybook version. I believe this issue happens to multiple layer folders structure as @jkincheloe33 mentioned above.
Storybook version 6.1 I also faced with alias problems, but fixed that using approach below.
TypeScript Storybook use different paths to tsconfig, for TsconfigPathsPlugin he uses tsconfig.app.json For redefine config, just add to main.js next code
SCSS
Also, there are two configs of webpack: managerWebpack and webpackFinal
I was struggling to find the solution and then i found yours solution. By the way i was missing a double quotes on webpackFinal. That’s all. Thanks
I’m running into this as well and I assume this will break a lot of Storybook devs experience.
This is what I had before that allowed to resolve to my root path properly, but for whatever reason this doesn’t help in my upgrade.
Here’s how I was able to resolve tsconfig path aliases in a React + TypeScript + Vite Storybook project, in case any other lost souls end up here: How to resolve aliases in Storybook? – Stackoverflow. The thread also details equivalent solutions for Webpack.
It works without slashes:
When I encountered this issue, I was expecting the aliases to resolve correctly for files in the
.storybook/directory. What I found was that it seems the aliases are not for resolving in the.storybook/directory (.storybook “config” files?), but instead for resolving when running the Storybook webpack build (npm run storybook).One thing we noted was that our custom React “add on” component, being registered in
.storybook/register.js, was being resolved like the other files in the.storybook/directory.We resolved our issue by using relative paths for
.storybook/files and addons, and are leveraging aliases, as described in this issue, for our actual Storybook story files.For our working Storybook webpack server aliases, we configured
.storybook/main.jslike:Thanks All!
trailing slash in alias key was the problem for me:
Late to the party, but I had to change my choice of character. I changed
$to@and it now works fineI’ve been doing some testing with this and there seems to be somewhat of an issue. I’ve setup a repo here to triage this. So far from my testing i saw a couple of things:
yarn storybook). But as soon as i issue a production build withyarn build-storybooki’m faced with the following error:tsconfig-paths-webpack-pluginusage and included a branch in the repo to do additional testing. Based on the configuration provided i’m faced with the following issue once i runyarn storybook:If anyone could share the repo, or take a look at the one i’ve mentioned we would have a better understanding of what’s going on and i could document this accurately.
Stay safe
hmmm, I can’t get why alias
@works but~is not… Spend pretty good amount of time trying to setup with~, thinking that something wrong with config itself… 🤯works fine:
SassError: SassError: Can’t find stylesheet to import: @import “~/assets/scss/utils”;
As per @TheDutchCoder , this works for me using Storyboard v6.2.9 and Vue 2. It configures an alias from
@to../srcand allows you to write relative imports from any folder, eg:Add a
webpackFinalkey to./.storybook/main.js:that’s what i thought. But in this case as we’re dealing with versioned documentation and what works for one, might not work for others. But this is a good start angular is covered. All that is missing is a way to handle the other frameworks.