Skip to content
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

Breaks on building Storybook #131

Closed
XiNiHa opened this issue Dec 18, 2023 · 20 comments
Closed

Breaks on building Storybook #131

XiNiHa opened this issue Dec 18, 2023 · 20 comments

Comments

@XiNiHa
Copy link

XiNiHa commented Dec 18, 2023

While building the Storybook, the bundler (Vite) emits an error like this:

[commonjs--resolver] No known conditions for "./native" specifier in "msw" package

This is because of the code here. While it'll work without any problem when not built, this will break on browser builds because...

  • The bundler doesn't know whether IS_REACT_NATIVE is true or false at build time, therefore it should try to bundle both versions.
  • However, since the bundler is building for browsers and that enables the browser export condition, the entry resolves to null and it errors.

This problem should be fixed by adding separate entries for each browser and native.

@belandrio
Copy link

I am facing the same issue

@YouHusam
Copy link

YouHusam commented Dec 20, 2023

I am having the same issue, are there any workarounds?

@satoshi-izawa
Copy link

I faced the same problem
node_modules/vite/node_modules/esbuild/lib/main.js:1374:21: ERROR: [plugin: vite:dep-pre-bundle] No known conditions for "./native" specifier in "msw" package

msw-storybook-addon: 2.0.0-beta.0
msw: 2.0.13
vite: 5.0.11

@mribichich
Copy link

for those using vite, I was able to fix the not found ./native module by adding this to vite.config

  resolve: {
    alias: {
      'msw/native': require.resolve(path.resolve(__dirname, './node_modules/msw/lib/native/index.mjs')),
    },
  },

@jetzhliu
Copy link

@marksulls
Copy link

marksulls commented Jan 22, 2024

Similar to the Vite specific fix above, this Webpack configuration is working for me (Angular / Storybook / Nx workspace):

in .storybook/main.ts:

import { StorybookConfig } from "@storybook/angular";
import * as path from "path";

const config: StorybookConfig = {
  stories: ['../**/*.stories.@(js|jsx|ts|tsx|mdx)'],
  addons: [
    '@storybook/addon-interactions',
    '@storybook/addon-docs',
    '@storybook/addon-essentials',
  ],
  framework: {
    name: '@storybook/angular',
    options: {},
  },
  staticDirs: ['../src/lib/msw'],
  // webpack workaround see https://github.com/mswjs/msw-storybook-addon/issues/131#issuecomment-1887930180
  webpackFinal: async (config) => {
    if (config.resolve) {
      config.resolve.alias = {
        ...config.resolve.alias,
        'msw/native': require.resolve(
          path.resolve(__dirname, '../../../node_modules/msw/lib/native/index.mjs')
        )
      };
    }
    return config;
  }
};
"msw": "2.1.3",
"msw-storybook-addon": "2.0.0-beta.0",

@henrybravos
Copy link

henrybravos commented Jan 29, 2024

worked for me:
//vite.config.ts

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { createRequire } from "node:module";
import path from "path";
const require = createRequire(import.meta.url);

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "msw/native": require.resolve(
        path.resolve(__dirname, "./node_modules/msw/lib/native/index.mjs")
      ),
    },
  },
});

@yannbf
Copy link
Collaborator

yannbf commented Jan 30, 2024

Hey everyone! If you're using MSW 2.0.0, we just released a new beta version that should have fixed this issue: [email protected].

Would you please try it out and let us know? Thank you!
cc @satoshi-izawa @marksulls tagging you since I know you're using msw 2!

@bmulholland
Copy link

For those who try it out, be very careful with your version specification in package.json. There's a "next" version of 2.0.0, 2.0.0-next.1, that is 9 months old -- but e.g. a version pin of >= 2.0.0-beta.1 will select that one. I suggest double checking your lockfile to see exactly what version you end up with.

@bmulholland
Copy link

Confirmed that the beta solves this issue for me 🎉

@magus
Copy link

magus commented Feb 3, 2024

The types aren't correct for the 2.0.0-beta1 but everything else works otherwise!

@chapmanio
Copy link

I've followed the vite-specific instructions here but I'm now getting this error:

[ERROR] No known conditions for "./node" specifier in "msw" package [plugin vite:dep-pre-bundle]

Is anyone having anything similar?

viteFinal: (config) => {
    config.resolve = {
      ...config.resolve,
      alias: {
        ...config.resolve?.alias,
        ["msw/native"]: require.resolve("../node_modules/msw/lib/native/index.mjs"),
      },
    };

    return mergeConfig(config, {
      plugins: [tsconfigPaths()],
    });
  },
    "msw": "^2.1.6",
    "msw-storybook-addon": "^2.0.0-beta.1",

@aronchick
Copy link

@chapmanio i solved that because you're using "msw/node" in something that is client code only. You may need to swap to "msw/browser" (but this will prevent you from using server libraries like fs or path).

@chapmanio
Copy link

@chapmanio i solved that because you're using "msw/node" in something that is client code only. You may need to swap to "msw/browser" (but this will prevent you from using server libraries like fs or path).

Do you have a code example of how to do that? I'm not importing MSW in my code (I assume that is being done by msw-storybook-addon) so it's not clear how to change this.

@glodynator
Copy link

I've followed the vite-specific instructions here but I'm now getting this error:

[ERROR] No known conditions for "./node" specifier in "msw" package [plugin vite:dep-pre-bundle]

Is anyone having anything similar?

viteFinal: (config) => {
    config.resolve = {
      ...config.resolve,
      alias: {
        ...config.resolve?.alias,
        ["msw/native"]: require.resolve("../node_modules/msw/lib/native/index.mjs"),
      },
    };

    return mergeConfig(config, {
      plugins: [tsconfigPaths()],
    });
  },
    "msw": "^2.1.6",
    "msw-storybook-addon": "^2.0.0-beta.1",

I'm having the same issue.

@acc-nicholas
Copy link

I'm having a similar issue: [vite] Internal server error: No known conditions for "./node" specifier in "msw" package

This works:

import { handlers } from "../mocks/handlers";

// Initialize MSW
initialize(
  {
    onUnhandledRequest: "warn",
  },
  [...handlers],
);

This does not work when importing from another lib in my monorepo.

import { handlers } from "@otherlib/mocks";

// Initialize MSW
initialize(
  {
    onUnhandledRequest: "warn",
  },
  [...handlers],
);

I have proper vite/typescript aliases/paths set up for this mocks lib btw.

"msw": "^2.2.1",
"msw-storybook-addon": "^2.0.0-beta.1",

@towry
Copy link

towry commented Mar 29, 2024

Fixed by fixing the dep version in package.json

"msw-storybook-addon": "2.0.0-beta.2",
"@mswjs/interceptors": ">=0.26.14",
"msw": "2.3.0-ws.rc-1"

@yannbf
Copy link
Collaborator

yannbf commented Apr 17, 2024

Hey everyone! Finally the major version is out. Sorry for such a long wait, I had to make sure the addon aligned with Storybook 8 and worked correctly in the portable stories (using stories in Vitest,Jest) scenario.

Please check the release notes, try it out and provide feedback in case issues arise:
https://github.com/mswjs/msw-storybook-addon/releases/tag/v2.0.0

Thanks!

@yannbf yannbf closed this as completed Apr 17, 2024
@rjaguilar
Copy link

rjaguilar commented Oct 31, 2024

I'm still getting this issue

[vite] Internal server error: No known conditions for "./node" specifier in "msw" package

"msw-storybook-addon": "2.0.3",
"msw": "2.4.3"

even while following

// .storybook/preview.js
-import { initialize, mswDecorator } from 'msw-storybook-addon'
+import { initialize, mswLoader } from 'msw-storybook-addon'

initialize()

const preview = {
-  decorators: [mswDecorator]
+  loaders: [mswLoader]
}

export default preview

what is the solution for this ?

@yannbf
Copy link
Collaborator

yannbf commented Nov 1, 2024

I'm still getting this issue

[vite] Internal server error: No known conditions for "./node" specifier in "msw" package

"msw-storybook-addon": "2.0.3", "msw": "2.4.3"

even while following

// .storybook/preview.js
-import { initialize, mswDecorator } from 'msw-storybook-addon'
+import { initialize, mswLoader } from 'msw-storybook-addon'

initialize()

const preview = {
-  decorators: [mswDecorator]
+  loaders: [mswLoader]
}

export default preview

what is the solution for this ?

That's quite odd! Could this be your node version? Could you check whether you have multiple MSW versions installed in your project? e.g. yarn why msw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests