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

Cannot find module msw/node after updating #1960

Open
4 tasks done
joshbenhamou opened this issue Jan 10, 2024 · 12 comments
Open
4 tasks done

Cannot find module msw/node after updating #1960

joshbenhamou opened this issue Jan 10, 2024 · 12 comments
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. potentially solved scope:node Related to MSW running in Node

Comments

@joshbenhamou
Copy link

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

21.4.0

Reproduction repository

https://github.com/mswjs/examples/tree/main/examples/with-vitest

Reproduction steps

The issue is happening in the official MSW Vitest example. Check out the sandbox link and see the typescript error https://codesandbox.io/p/sandbox/github/mswjs/examples-new/tree/main/examples/with-vitest?file=%2Fmocks%2Fnode.ts%3A2%2C32-3%2C1

Current behavior

Import validation fails

Expected behavior

Import validation succeeds

@joshbenhamou joshbenhamou added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Jan 10, 2024
@kettanaito
Copy link
Member

Hi, @joshbenhamou. I've just updated all the examples to the latest version of MSW and they all pass. Could you please confirm your issue is solved?

@joshbenhamou
Copy link
Author

Thanks @kettanaito . The sandbox link is no longer working for me in https://github.com/mswjs/examples/tree/main/examples/with-vitest. However, I saw that you updated the MSW package to 2.0.14 and tried that locally. Same problem with that version and also with 2.1.3

@kettanaito
Copy link
Member

kettanaito commented Jan 25, 2024

Oh, thanks for pointing out the broken sandboxes! Caused by renaming the repo and deleting the old one. Will fix.

Edit: the sandbox is running properly now but it doesn't seem CodeSandbox allows us to use terminal anymore to run tests. You may want to check that example locally.

@joshbenhamou
Copy link
Author

Thanks. The sandbox is working for me now. It looks like the change you've made that is the module and moduleResolution in the tsconfig.json. Trying this locally, raises typescript errors across every import. I need to look into this further but happy for you to close the issue as it's working on the demo. If you could explain a little behind the rationale of the compiler option changes that'd be nice though

@iChip
Copy link

iChip commented Jan 26, 2024

I'm experiencing the same issue, but no changes to module/moduleResolution seem to resolve it in my project.

I do notice that the sandbox is a major version behind on both vitest and resolved vite version.. that being said, after bumping these, I still cannot reproduce the issue in the sandbox 🙃

@joshbenhamou
Copy link
Author

Same as @iChip , it's still not working for me but I haven't had the time to investigate yet unfortunately

@sampolahtinen
Copy link

Same thing happens for me. My setup is Remix, Cloudflare Workers, Playwright with following configs:

    ...
    "moduleResolution": "Bundler",
    "module": "ES2022",
    "resolveJsonModule": true,
    "target": "ES2022",
    ...
/** @type {import('@remix-run/dev').AppConfig} */
export default {
  ignoredRouteFiles: [`**/.*`],
  server: `./app/server.ts`,
  serverConditions: ['workerd', 'worker', 'browser'],
  serverDependenciesToBundle: [
    // bundle everything except the virtual module for the static content manifest provided by wrangler
    /^(?!.*\b__STATIC_CONTENT_MANIFEST\b).*$/,
  ],
  serverMainFields: [`browser`, `module`, `main`],
  serverMinify: true,
  serverModuleFormat: `esm`,
  serverPlatform: `neutral`,
  browserNodeBuiltinsPolyfill: {
    modules: {
      buffer: true,
    },
  },
  serverNodeBuiltinsPolyfill: {
    modules: ['util', 'crypto'],
  },
}

Do we have any ideas what might be the cause?

I am willing to give a stab and fixing it if there is a person who could point me to a direction!

@DamienDeloubes
Copy link

DamienDeloubes commented May 2, 2024

Just want to chime and and mention I am upgrading MSW from 1.3.3 to 2.2.13 and receive the same error

Package path ./node is not exported from package /Users/damien/Documents/Git/Maia/monorepo/node_modules/msw (see exports field in /Users/damien/Documents/Git/Maia/monorepo/node_modules/msw/package.json)

My scripts is very basic

import { setupServer, type SetupServer } from 'msw/node';

import { handlers } from './handlers';

export const server: SetupServer = setupServer(...handlers);

@erichartline
Copy link

I'm also running into the same issue after upgrading. My use case is to mock out API responses for components using Storybook and the MSW addon. I tried upgrading everything together -- MSW to v2, msw-storybook-addon to v2, Storybook from v7 to v8 -- but no matter what I try I'm getting the same error as @DamienDeloubes.

I tried the hacky solution mentioned in another issue but that still caused errors with any component's Storybook file that was using the MSW addon. I've tried adjusting my tsconfig.json and tweaking my Storybook Webpack config to no avail. I'm also not using the app router so there's no conflict there. Does anyone have any other ideas?

@NateWilliams2
Copy link

IDK if this will help you, but I had a similar issue and found that the hacky solution only worked if I removed the !isServer clause, i.e. not adding an alias for 'msw/node' for the client build. I posted my solution at #1877 (comment)

@erichartline
Copy link

IDK if this will help you, but I had a similar issue and found that the hacky solution only worked if I removed the !isServer clause, i.e. not adding an alias for 'msw/node' for the client build. I posted my solution at #1877 (comment)

Thanks for the idea. Unfortunately, I'm still getting the Package path ./node is not exported from package node_modules/msw when trying to access any stories using MSW. I've tried every variation of the Webpack config I can think of to no avail. Glad you were able to find a solution though!

@kettanaito
Copy link
Member

With the help from @phryneas, we've done some improvements in the export conditions in MSW. Please upgrade to msw@2.3.0 and let me know if you still experience the Package path ./node is not exported from package node_modules/msw error. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs:triage Issues that have not been investigated yet. potentially solved scope:node Related to MSW running in Node
Projects
None yet
Development

No branches or pull requests

7 participants