Skip to content

'Error: No known conditions for "./node" specifier in "msw" package' when using MSW with Vitest #125

Open

Description

@cat841

Trying to import and use any function from MSW (specifically, msw/node) when running vitest prints this error:

 FAIL  src/testfile.test.ts [ src/testfile.test.ts ]
Error: No known conditions for "./node" specifier in "msw" package
 ❯ e node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:21445:25
 ❯ n node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:21445:646
 ❯ o node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:21445:1297
 ❯ resolveExportsOrImports node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28746:20
 ❯ resolveDeepImport node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28765:31
 ❯ tryNodeResolve node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28453:20
 ❯ Context.resolveId node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:28212:28
 ❯ Object.resolveId node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:44276:32
 ❯ TransformContext.resolve node_modules/.pnpm/vite@4.5.0_@types+node@20.8.10/node_modules/vite/dist/node/chunks/dep-bb8a8339.js:43992:23

This only happens when this plugin is present, removing it seems to fix the issue.

Not sure if this is a bug in the plugin, or a problem on msw's side, or if I'm maybe doing something wrong?

Activity

  1. sanghunjlee commented on Mar 4, 2024

    @sanghunjlee

    Had the same issue earlier today.

    For those visiting this issue searching for solution, I went around this issue by adding an alias to resolve in the config file:

    // vite.config.ts
    
    export default defineConfig({
      // ...otherConfigs,
      resolve: {
        alias: [
          { find: "msw/node", replacement: "/node_modules/msw/lib/native/index.mjs"}
        ],
      }
    })

    Hope that helps.

  2. nknapp commented on Mar 19, 2024

    @nknapp

    I created an mswjs/msw#2092 at msw, but they said it's a problem of the framework (i.e. solid).

    I still have to try the workaround they proposed, but the reason for the error is that vite-plugin-solid sets "browser" as resolve.conditions here.

    This means that vite tries to load the browser-version of msw/node which does not exist.

    According to @kettanaito it is wrong to use browser for node.js, I personally think it is debatable.

    What I also noticed is that solid returns and empty render function when the resolve.conditions is set to "node". I don't really undestand why, but it seems wrong to assume that Node.js is only ever used for SSR.

  3. nknapp commented on Mar 23, 2024

    @nknapp

    So, after some more insights, my last statement is wrong. My current stance is that the package.json of mock-service-worker is not entirely correct. The resolve.conditions applied in this module actuall contains node. So it should be possible to load msw/node

  4. brenelz commented on Sep 11, 2026

    @brenelz
    Contributor

    Can you retest with updated MSW/Vite/Vitest?

  5. kettanaito commented on Sep 14, 2026

    @kettanaito

    There's been a ton of changes and improvements to both Vitest and MSW since this was open. I highly recommend trying to reproduce this on the latest versions of both libs. There's a very small chance it will still happen. We are testing all MSW packages in Vitest. I also know for a fast Vitest doesn't use the browser export condition unless you force it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions