question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next example not using 'require'

See original GitHub issue

Is your feature request related to a problem? Please describe. We are using next js w/ msw. In the example posted here: https://github.com/vercel/next.js/blob/canary/examples/with-msw/pages/_app.js#L1

a require() is done which adds msw to the bundle. We are using a lot of big json files for fixture data in conjunction w/ our msw endoints. And the use of require here is causing our bundle size to balloon.

Describe the solution you’d like Looking for a way to safely load msw before any react renders that doesn’t use require.

Describe alternatives you’ve considered await import() but that seems to cause race conditions where msw doesn’t load in time (i.e. before react tries to fetch)

Additional context Add any other context or screenshots about the feature request here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ismaycommented, May 30, 2021

I confirm that the Official NextJS usage example does not include msw package in its production bundle. See the bundle analyzer screenshot below.

@kettanaito There seems to have been a regression in the next.js build process as now msw is being included in the bundle. I’ve opened an issue for it with next.js here: https://github.com/vercel/next.js/issues/25607 (note that I tested this by removing .env.production from the example, as otherwise you would expect msw to be present in the bundle, since NEXT_PUBLIC_API_MOCKING would be enabled). See the linked issue for more details.

0reactions
kettanaitocommented, Sep 11, 2020

I confirm that the Official NextJS usage example does not include msw package in its production bundle. See the bundle analyzer screenshot below.

Screen Shot 2020-09-11 at 09 46 50

Note: You have to set the NEXT_PUBLIC_API_MOCKING environmental variable to "disabled" in your .env file, otherwise you are bundling a production app with the mocking enabled, which intentionally includes msw in the build.

If you have a custom build setup make sure you are importing msw conditionally so it doesn’t end up in the production bundle unused. If you want to run msw in production intentionally it must be included by design in order to run.

Read more comments on GitHub >

github_iconTop Results From Across the Web

require is not defined? Node.js - javascript - Stack Overflow
It happens when you declare your package type as module in your package.json . If you do this, certain CommonJS variables can't be...
Read more >
module-not-found - Next.js
The module you're trying to import uses Node.js specific modules, for example dns , outside of getStaticProps / getStaticPaths / getServerSideProps. Possible ...
Read more >
How To Fix ReferenceError require is not defined in JavaScript
You are using require in a Node.​​ In this case, check your package. json file for an property called type . If that...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/package require() of ES modules is not supported. ES module file as it ...
Read more >
Module not found: Can't resolve 'fs' · Issue #7755 · vercel/next.js
Bug report Describe the bug I'm using the popular find-up npm package, which has locate-path as a dependency. locate-path requires fs within ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found