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.

'Module not found' when using twin.macro in MDX files

See original GitHub issue

I was just upgrading to Storybook 6 and the stories won’t compile anymore. It looks like twin.macro is the culprit, but I’m not sure. So, I created a new CRA project, added tailwindcss, twin.macro, styled-components and typescript. I copied over tailwind.config.js, the twin.macro config, and one simple Badge.tsx and Badge.stories.mdx file. Both of the stories files use some <div tw="..."> stuff. I managed to reproduce the errors but everything seems to compile again when I remove the twin.macro import from the MDX file.

In the terminal I see first some warnings

WARNING in ./node_modules/babel-plugin-macros/dist/index.js 78:24-31 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted @ ./node_modules/twin.macro/macro.js

Same for “WARNING in ./node_modules/twin.macro/macro.js 40:61-80” and “WARNING in ./node_modules/babel-plugin-macros/node_modules/import-fresh/index.js 31:31-48”

Then errors:

ERROR in ./node_modules/babel-plugin-macros/node_modules/cosmiconfig/dist/readFile.js Module not found: Error: Can’t resolve ‘fs’ in ‘/home/fvanwijk/projects/storybook-test/node_modules/babel-plugin-macros/node_modules/cosmiconfig/dist’

(and more similar errors)

So my questions are:

  1. Could you reproduce this issue?
  2. Could you confirm the issue is caused by twin.macro not being compatible with storybook 6 when using MDX files?
  3. Do you know how to fix?

As workaround I could extract all story boilerplate into tsx files, but I find it quite cumbersome to do this for something simple like 3 component examples in a grid.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ben-rogersoncommented, Nov 18, 2020

I’ve just added a storybook + emotion demo, more soon.

1reaction
matthiskcommented, Aug 20, 2020

I found the issue. In Storybook 6.0 the stories.mdx files still use the babel-loader. However the loader now disables the babelrc configuration. This probably has the side-effect of not loading our bableMacros config from the package.json. When I remove the babelrc and configFile field from the webpack config, tw.macro works correctly in mdx again. I wonder why Storybook decided to disable babelrc in the loader?

{
  "test": "/(stories|story).mdx/",
  "use": [
    {
      "loader": "babel-loader/lib/index.js",
      "options": {
        "babelrc": false,
        "configFile": false
       }
    }
  ]
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found error in twin.macro and nextjs
I am trying to build a project using nextjs and plan to use twin.macro but i get error like this everytime: TypeError…
Read more >
Module not found: Can't resolve 'fs' in Next.js application
The Module not found: can't resolve 'xyz' error happens when you try to use server only code outside of these methods. Error example...
Read more >
gatsby-starter-twin-macro-mdx
matiasfha/gatsby-starter-twin-macro-mdx. Environmentgatsby. This sandbox is in sync with main on GitHub. You have to fork to make changes. Fork. Files.
Read more >
Developer Portfolio with Gatsby, Tailwind, & MDX ft. Matías ...
Learn how to use Gatsby, Tailwind, & MDX to build a developer portfolio!In this stream, Matías Hernández walks through :0:00:00​ - Intro to ......
Read more >
A Walkthrough of migrating MaxRozen.com from Gatsby to ...
For styling, I just used Emotion (CSS in JS) with Tailwind via twin.macro (you can ... Whereas in Gatsby I had a templates/article.js...
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