next.js: Cannot read properties of undefined (reading 'listen') when importing faker
See original GitHub issuePrerequisites
- I confirm my issue is not in the opened issues
- I confirm the Frequently Asked Questions didn’t contain the answer to my issue
Environment check
- I’m using the latest
msw
version - I’m using Node.js version 14 or higher
Node.js version
v17.8.0
Reproduction repository
https://github.com/djaustin/msw-faker-reproduction/tree/with-faker
Reproduction steps
yarn && yarn dev
Visit http://localhost:3000
To revert to a working configuration, checkout branch ‘working-factory’ (https://github.com/djaustin/msw-faker-reproduction/tree/working-factory)
The import of faker seems to cause the error to occur.
Current behavior
An error is displayed in the console and the browser.
TypeError: Cannot read properties of undefined (reading 'listen')
at eval (webpack-internal:///./mocks/index.js:3:12)
at Object../mocks/index.js (/Users/daustin/development/msw/.next/server/pages/_app.js:32:1)
at __webpack_require__ (/Users/daustin/development/msw/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///./pages/_app.js:9:5)
at Object../pages/_app.js (/Users/daustin/development/msw/.next/server/pages/_app.js:65:1)
at __webpack_require__ (/Users/daustin/development/msw/.next/server/webpack-runtime.js:33:42)
at __webpack_exec__ (/Users/daustin/development/msw/.next/server/pages/_app.js:130:39)
at /Users/daustin/development/msw/.next/server/pages/_app.js:131:28
at Object.<anonymous> (/Users/daustin/development/msw/.next/server/pages/_app.js:134:3)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
TypeError: Cannot read properties of undefined (reading 'listen')
at eval (webpack-internal:///./mocks/index.js:3:12)
at Object../mocks/index.js (/Users/daustin/development/msw/.next/server/pages/_app.js:32:1)
at __webpack_require__ (/Users/daustin/development/msw/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///./pages/_app.js:9:5)
at Object../pages/_app.js (/Users/daustin/development/msw/.next/server/pages/_app.js:65:1)
at __webpack_require__ (/Users/daustin/development/msw/.next/server/webpack-runtime.js:33:42)
at __webpack_exec__ (/Users/daustin/development/msw/.next/server/pages/_app.js:130:39)
at /Users/daustin/development/msw/.next/server/pages/_app.js:131:28
at Object.<anonymous> (/Users/daustin/development/msw/.next/server/pages/_app.js:134:3)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
error - mocks/index.js (3:9) @ eval
TypeError: Cannot read properties of undefined (reading 'listen')
1 | if (typeof window === 'undefined') {
2 | const { server } = require('./server')
> 3 | server.listen()
| ^
4 | } else {
5 | const { worker } = require('./browser')
6 | worker.start()
Expected behavior
msw successfully creates a new object from the factory model and returns it for display in the application
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
cannot read properties of undefined (reading 'listen') - You.com
The import of faker seems to cause the error to occur. Current behavior. An error is displayed in the console and the browser....
Read more >Cannot read properties of undefined (reading 'call') on Next.js ...
I'm sure the error is not about the URL as I already added the domain to be whitelisted in my next.config.js file. Here...
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 >Changelog - Cypress Documentation
Cypress no longer throws the error "cannot read property split of undefined" in certain circumstances when application errors are thrown. Fixes #17378.
Read more >Suspense for Data Fetching (Experimental) - React
React 16.6 added a <Suspense> component that lets you “wait” for some code to load and declaratively specify a loading state (like a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If you inspect the return of the
require('./server')
you will notice that it returns a Promise:Something in the import chain forces the
./server
module to be imported asynchronously. I believe that’s not the nativerequire
behavior (I’m also running Node.js v14.18.1).If you rewrite
require
toimport
and await it, the issue gets resolved:If this issue occurs only when you introduce a certain dependency, like faker, I suspect that dependency somehow affects the import type your module uses. Sorry, I don’t have the capacity to look deeper into this but the
import
proposal above is functional and should unblock you.Anybody having more experience with module resolution is welcome to write a more constructive explanation below. Thank you!
Shouldn’t the doc be updated here https://mswjs.io/docs/getting-started/integrate/node? Maybe at least to mention that in the context of Next.js it’s better to use async/await to avoid that
undefined
error.