msw-storybook-addon icon indicating copy to clipboard operation
msw-storybook-addon copied to clipboard

Storybook-Vite-Builder: requested module does not provide an export

Open MIreland opened this issue 2 years ago • 6 comments

I'm attempting to use msw-storybook-addon with storybook-builder-vite, and running into the following error:

image

Any suggestions on how to resolve this?

I'll aim to get a code sandbox up if I get a chance

MIreland avatar Oct 06 '21 21:10 MIreland

Hey @MIreland thanks for opening this issue! I haven't really used this addon with vite so it would be incredibly helpful if you could provide a repro for it! Let me know and I'll try to figure out what's going on :)

yannbf avatar Oct 09 '21 22:10 yannbf

@yannbf, this may be related to the issue: https://github.com/mswjs/msw/issues/670

I'm running the reproduction repo above and get the same error:

Uncaught SyntaxError: The requested module './../node_modules/msw-storybook-addon/dist/mswDecorator.js' does not provide an export named 'initialize'

When inspecting the published msw-storybook-addon, I can also see that it doesn't export all the things properly from its main module (there's only mswDecorator variable, everything else must be accessed via 'msw-storybook-addon/dist/mswDecorator', which is not the intention).

kettanaito avatar Nov 24 '21 16:11 kettanaito

This looks like a bundler issue at first glance. The module is installed, it does export the things I'm importing in .storybook/preview.js. The things that bundles preview.js throws the exception. It may be connected to virtually anything: wrong module resolution, incompatible targets, internal specifics of tools. We need to find a way to narrow the problematic surface down...

kettanaito avatar Nov 24 '21 16:11 kettanaito

Vite, just like webpack 5, does not provide node polyfills. There is some specific code to make it work for webpack 5, it may need to be extended to support other bundlers :/

cf https://github.com/mswjs/msw-storybook-addon/blob/361418ed803f0dbbbab13f864f29b62ffb71d518/packages/msw-addon/src/mswDecorator.ts#L32

esquevin avatar Mar 17 '22 09:03 esquevin

This often happens for me the msw addon and vite builder but if I stop and start storybook it works again

dannyhw avatar Jun 17 '22 12:06 dannyhw

If you are using Vite: 4.3.9, and version 1.8 of this plugin for Storybook 7. I was able to get it to work by adding the following code to vite.config.ts

import replace from '@rollup/plugin-replace'

export const config: UserConfigExport = {
  plugins: [
    replace({
      '_globalThis$process.env.NODE_ENV': JSON.stringify('production'),
    }),
    react(),
// the rest

CurtisHumphrey avatar Jun 23 '23 05:06 CurtisHumphrey