question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Storybook is not able to resolve path with Next js absolute import

See original GitHub issue

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.

const path = require('path');
const resolve = (dir) => path.resolve(__dirname, dir);

module.exports = ({ config }) => {
  config.resolve.alias = {
    '@utils/*': resolve('../src/utils/*'),
   ....
  };

  return config;
};

Error


ERROR in ./src/components/Base/Button/Button.jsx
Module not found: Error: Can't resolve '@utils/boxShadowGenerator' in '/Users/rahulshrivastava/projects/unacademy-web-learning/src/components/Base/Button'
 @ ./src/components/Base/Button/Button.jsx 9:0-59 107:35-53 122:9-27
 @ ./src/stories/button.stories.js
 @ ./src/stories sync \.stories\.js$
 @ ./.storybook/config.js
 @ multi ./node_modules/@storybook/core/dist/server/common/polyfills.js ./node_modules/@storybook/core/dist/server/preview/globals.js ./.storybook/config.js (webpack)-hot-middleware/client.js?reload=true&quiet=true

Code snippets I am using jsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2016",
    "jsx": "react",
    "baseUrl": "./src/",
    "paths": {
      "@utils/*": ["utils/*"],
    ....
    }
  },
  "exclude": ["node_modules", "dist"]
}

System:

 npx -p @storybook/cli@next sb info

Environment Info:
(node:1828) UnhandledPromiseRejectionWarning: TypeError: e.filter is not a function
    at /Users/rahulshrivastava/.npm/_npx/1031/lib/node_modules/@storybook/cli/node_modules/envinfo/dist/envinfo.js:1:73205
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async Promise.all (index 6)
(node:1828) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1828) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:31 (11 by maintainers)

github_iconTop GitHub Comments

71reactions
GxDesigncommented, Sep 10, 2020

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”:

config.resolve.modules = [
      path.resolve(__dirname, ".."),
      "node_modules",
    ]

But it does not tell webpack anything about the “@/Components” syntax. You would need to pass an alias configuration like this:

config.resolve.alias = {
      ...config.resolve.alias,
      "@/Components": path.resolve(__dirname, "../src/components")
    };
64reactions
GxDesigncommented, Sep 9, 2020

I havent gotten everything working with Next yet but this resolved the absolute import issues (in .storybook/webpack.config.js):

const path = require('path');

module.exports = ({config}) => {
  config.resolve.modules = [
    path.resolve(__dirname, ".."),
    "node_modules",
  ]

  return config
}

For using ./src, you can use path.resolve(__dirname, “…”, “src”),

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storybook with absolute paths - Stack Overflow
I got this issue after using CLI and I was able to resolve it by modifying my .storybook/main.js to: const path = require('path'); ......
Read more >
Storybook is not able to resolve path with Next js absolute ...
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not...
Read more >
How to resolve a path alias in Storybook - +return
The solution. To solve this issue, we will need to configure Storybook's webpack to resolve the path alias. the concept is really similar...
Read more >
Storybook Addon Next
A no config Storybook addon that makes Next.js features just work in Storybook. ... Sass/Scss; Css/Sass/Scss Modules; Styled JSX; Postcss; Absolute Imports ......
Read more >
[Solved]-Storybook with absolute paths-Reactjs
Related Query · Storybook with absolute paths · Jest gives `Cannot find module` when importing components with absolute paths · Resolve Absolute /...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found