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

Webpack 5 Throws ModuleNotFoundError #47

Closed
KL13NT opened this issue Aug 23, 2021 · 15 comments · Fixed by #53
Closed

Webpack 5 Throws ModuleNotFoundError #47

KL13NT opened this issue Aug 23, 2021 · 15 comments · Fixed by #53

Comments

@KL13NT
Copy link

KL13NT commented Aug 23, 2021

Hi, I'm using Webpack^5.46.0, @storybook/react^6.3.4, msw-storybook-addon^1.2.0, and msw^0.35.0. I have Storybook configured to use Webpack 5. Whenever the Storybook dev server is running and I modify the preview to initialize the worker, it works. But when I shutdown the server and restart it, it breaks with the following error:

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it

This error is thrown multiple times for multiple packages. Has this been reported before? Any possible fix?

@KL13NT
Copy link
Author

KL13NT commented Aug 23, 2021

Right now what I do to get this to not throw is remove the addon's decorator and initialize code from preview.js, wait for the dev-server to startup successfully, and then change preview.js.

If I add the addon's initialize code before starting the server, it throws.

@mouhannad-sh
Copy link

mouhannad-sh commented Aug 24, 2021

Went through the same thing just now...
following the error traces. I tried this and it worked for me (looking for a better solution :D )

I followed the setup guide from storybook to support webpack 5

my deps look like this now:

"devDependencies": {
    "@storybook/addon-actions": "^6.4.0-alpha.11",
    "@storybook/addon-essentials": "^6.4.0-alpha.11",
    "@storybook/addon-links": "^6.4.0-alpha.11",
    "@storybook/builder-webpack5": "^6.4.0-alpha.11",
    "@storybook/manager-webpack5": "^6.4.0-alpha.11",
    "@storybook/react": "^6.4.0-alpha.11",
    "msw": "^0.35.0",
    "msw-storybook-addon": "^1.2.0"
},
"resolutions": {
    "webpack": "^5.45.1",
    "css-loader": "^5.0.0",
    "dotenv-webpack": "^6.0.0",
    "html-webpack-plugin": "^5.3.2",
    "style-loader": "^2.0.0",
    "terser-webpack-plugin": "^5.1.4",
    "webpack-dev-middleware": "^4.1.0",
    "webpack-virtual-modules": "^0.4.2",
  }

Then in .storybook/main.js I have a custom webpackFinal setup to modify SB's pollyfil configs

webpackFinal: config => {
    return {
      ...config,
      resolve: {
        ...config.resolve,
        modules: [path.resolve("./src"), ...config.resolve.modules],
        fallback: {
           timers: false,
           tty: false,
           os: false,
           http: false,
           https: false,
           zlib: false,
           util: false,
           ...config.resolve.fallback,
        }
      }
    };
  }

Nowadays setting up Storybook feels like performing surgery !

@KL13NT
Copy link
Author

KL13NT commented Aug 28, 2021

Thanks @mouhannad-sh, this worked perfectly for me. I suppose mswjs depends on some Node modules even when using it in the browser which explains why Webpack's fallback defaults were needed previously.

@kettanaito
Copy link
Member

kettanaito commented Sep 6, 2021

Is there any stack trace to these errors?

MSW mustn't use any Node.js modules in a browser. We have two separate builds for the browser and Node.js. As long as you don't import msw/node you won't get any Node.js-related logic (the name implies).

Can somebody confirm that a clean Storybook setup with webpack 5 behaves properly without the polyfills but adding this add-on results in errors?

@sklawren
Copy link

sklawren commented Sep 15, 2021

I just upgraded my project from gatsby 2 to 3, which switches to webpack 5.

I followed all the instructions in the storybook webpack 5 documentation (with Shilman's help), but this error kept happening.

I noticed the @mswjs in the ModuleNotFoundError, commented out the msw initialization code in preview.js

//import {initialize, mswDecorator} from 'msw-storybook-addon';
//initialize();
//addDecorator(mswDecorator);

After that, Storybook launched without issue.

I googled and found this thread on github. I followed @mouhannad-sh instructions, uncommented the msw storybook code and everything works again.

Here is the stack trace (it repeats this error multiple times, so I only pasted the first occurrance).

manager (webpack 5.52.1) compiled successfully in 13889 ms
99% done plugins webpack-hot-middlewarewebpack built preview 34cdf0e9d905502cac0d in 19490ms
ModuleNotFoundError: Module not found: Error: Can't resolve 'http' in '/Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "http": require.resolve("stream-http") }'
	- install 'stream-http'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "http": false }
    at /Users/steven/Development/project/node_modules/webpack/lib/Compilation.js:1773:28
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:814:13
    at eval (eval at create (/Users/steven/Development/project/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:10:1)
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:286:22
    at eval (eval at create (/Users/steven/Development/project/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:9:1)
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:442:22
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:124:11
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:676:25
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:885:8
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:1005:5
    at /Users/steven/Development/project/node_modules/neo-async/async.js:6883:13
    at /Users/steven/Development/project/node_modules/webpack/lib/NormalModuleFactory.js:988:45
    at finishWithoutResolve (/Users/steven/Development/project/node_modules/enhanced-resolve/lib/Resolver.js:296:11)
    at /Users/steven/Development/project/node_modules/enhanced-resolve/lib/Resolver.js:362:15
    at /Users/steven/Development/project/node_modules/enhanced-resolve/lib/Resolver.js:410:5
    at eval (eval at create (/Users/steven/Development/project/node_modules/enhanced-resolve/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:16:1)
resolve 'http' in '/Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest'
  Parsed request is a module
  using description file: /Users/steven/Development/project/node_modules/@mswjs/interceptors/package.json (relative path: ./lib/interceptors/ClientRequest)
    Field 'browser' doesn't contain a valid alias configuration
    resolve as module
      looking for modules in /Users/steven/Development/project/src
        single file module
          using description file: /Users/steven/Development/project/package.json (relative path: ./src/http)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.js doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.jsx doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.tsx doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.json doesn't exist
            .cjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/src/http.cjs doesn't exist
        /Users/steven/Development/project/src/http doesn't exist
      /Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/node_modules doesn't exist or is not a directory
      /Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/interceptors/node_modules doesn't exist or is not a directory
      /Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/node_modules doesn't exist or is not a directory
      looking for modules in /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules
        single file module
          using description file: /Users/steven/Development/project/node_modules/@mswjs/interceptors/package.json (relative path: ./node_modules/http)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.js doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.jsx doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.tsx doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.json doesn't exist
            .cjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http.cjs doesn't exist
        /Users/steven/Development/project/node_modules/@mswjs/interceptors/node_modules/http doesn't exist
      /Users/steven/Development/project/node_modules/@mswjs/node_modules doesn't exist or is not a directory
      /Users/steven/Development/project/node_modules/node_modules doesn't exist or is not a directory
      looking for modules in /Users/steven/Development/project/node_modules
        single file module
          using description file: /Users/steven/Development/project/package.json (relative path: ./node_modules/http)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.js doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.jsx doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.tsx doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.json doesn't exist
            .cjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/node_modules/http.cjs doesn't exist
        /Users/steven/Development/project/node_modules/http doesn't exist
      /Users/steven/Development/amex/node_modules doesn't exist or is not a directory
      /Users/steven/Development/node_modules doesn't exist or is not a directory
      /Users/steven/node_modules doesn't exist or is not a directory
      /Users/node_modules doesn't exist or is not a directory
      /node_modules doesn't exist or is not a directory
      looking for modules in /Users/steven/Development/project/test
        single file module
          using description file: /Users/steven/Development/project/package.json (relative path: ./test/http)
            no extension
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http doesn't exist
            .mjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.mjs doesn't exist
            .js
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.js doesn't exist
            .jsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.jsx doesn't exist
            .ts
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.ts doesn't exist
            .tsx
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.tsx doesn't exist
            .json
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.json doesn't exist
            .cjs
              Field 'browser' doesn't contain a valid alias configuration
              /Users/steven/Development/project/test/http.cjs doesn't exist
        /Users/steven/Development/project/test/http doesn't exist
ModuleNotFoundError: Module not found: Error: Can't resolve 'http' in '/Users/steven/Development/project/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest'

@kettanaito
Copy link
Member

Based on the stack trace, the issue is caused by the client-side Storybook loading a Node.js version of MSW (setupServer). That latter rightfully relies on Node.js built-ins like http because it's designed to run in that environment.

@yannbf, do you think this may be an issue we've introduced with the worker/server dual support recently?

@yannbf
Copy link
Collaborator

yannbf commented Sep 16, 2021

Based on the stack trace, the issue is caused by the client-side Storybook loading a Node.js version of MSW (setupServer). That latter rightfully relies on Node.js built-ins like http because it's designed to run in that environment.

@yannbf, do you think this may be an issue we've introduced with the worker/server dual support recently?

That seems like it! However I don't understand why it would happen if that code is supposed to be lazy loaded 🤔
I'd love to check about this issue, though I'm not sure how to reproduce it. Can anyone provide a minimal repro?

Edit: I managed to reproduce it. Will investigate further!

@donferi
Copy link

donferi commented Sep 29, 2021

Hi @yannbf by any chance do you have a minimal repro? I'm experiencing the same issue in a different non-storybook repo but I haven't been able to isolate the issue. For some reason the server code is getting bundled even if my require is behind a conditional 🤔

@yannbf
Copy link
Collaborator

yannbf commented Oct 9, 2021

Hey @donferi the thing is that Webpack can't really tell whether a node code will run at runtime or not, so even if you are 100% sure the node code will not run on the browser, Webpack will still try to process it. In Webpack 4 the node polyfills were added, but they are removed in Webpack 5 so we either need to add them manually, or make sure that Webpack will ignore the require/imports.

There's a long discussion about that here if you'd like to get more context about it!

@kettanaito
Copy link
Member

The issue should be fixed by #53.

@chiptus
Copy link

chiptus commented Feb 24, 2022

The issue should be fixed by #53.

still get the same issue

@kettanaito
Copy link
Member

Please update to msw@latest.

@chiptus
Copy link

chiptus commented Feb 24, 2022

my problem was with using the wrong rest

@ninjarogue
Copy link

ninjarogue commented Mar 4, 2022

I'm having a similar issue (mode not found errors when running app on the browser) but without storybook. I'm importing a type from msw/node, could this issue have something to do with that?

Here is an example of the errors I'm seeing on the react-error-overlay:

Compiled with problems:

ERROR in ./node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/NodeClientRequest.js 224:13-28

Module not found: Error: Can't resolve 'http' in '/Users/aricjiang/dev/automated-testing-react/node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add a fallback 'resolve.fallback: { "http": require.resolve("stream-http") }'
	- install 'stream-http'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.fallback: { "http": false }

Here's the code that I think may be responsible for these errors:

// mockNetwork.ts

import { handlers } from './handlers';
import type { SetupWorkerApi } from 'msw';
import type { SetupServerApi } from 'msw/node';



type TNetworkType = 'worker' | 'server';

interface IMockNetwork {
  type: TNetworkType;
  worker?: SetupWorkerApi;
  server?: SetupServerApi;
}

export function __generateMockNetwork(): IMockNetwork {
  return process.env.NODE_ENV === 'development' ?
    __setupWorker()
    :
    __setupServer();
}



function __setupWorker(): IMockNetwork {
  const { setupWorker } = require('msw');

  return {
    type: 'worker',
    worker: setupWorker(...handlers)
  };
}



function __setupServer(): IMockNetwork {
  const { setupServer } = require('msw/node');

  return {
    type: 'server',
    server: setupServer(...handlers)
  };
}



export const mockNetwork = __generateMockNetwork();

@kettanaito
Copy link
Member

kettanaito commented Mar 8, 2022

Hi, @ninjarogue. I believe I've replied to you in another thread. You have the mistake of missing setupWorker and setupServer in the same module. Those functions are meant for different environments and you cannot import them in a single module. Please split them by modules as shown in the examples and the issue will be gone.

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

Successfully merging a pull request may close this issue.

8 participants