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

Nextjs 13.4.9 - Error: Cannot find module 'webpack' #52366

Closed
1 task done
alifa20 opened this issue Jul 7, 2023 · 19 comments · Fixed by #52587
Closed
1 task done

Nextjs 13.4.9 - Error: Cannot find module 'webpack' #52366

alifa20 opened this issue Jul 7, 2023 · 19 comments · Fixed by #52587
Assignees
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked

Comments

@alifa20
Copy link

alifa20 commented Jul 7, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.16.1
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: 8.6.6
    Relevant Packages:
      next: 13.4.9-canary.3
      eslint-config-next: 13.4.9
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue or a replay of the bug

alifa20/next-js-demo#1

To Reproduce

  • Git pull the repo
  • yarn && yarn dev
  • Try browsing localhost:3000
  • The error will come up in the console

Describe the Bug

I have a suspicion that this issue may be related to the following GitHub links:

It's unclear at the moment, so I'm not entirely certain.
However, the older version, v13.4.8, seems to be functioning properly.

Expected Behavior

Localhost:3000 should come up.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1434

@alifa20 alifa20 added the bug Issue was opened via the bug report template. label Jul 7, 2023
@Marcus-Rise
Copy link
Contributor

In production build runs ok, but in dev it throws this error in my pull request with upgrade

Dev mode error in console
/usr/local/bin/yarn run dev
yarn run v1.22.17
$ next dev
- ready started server on 0.0.0.0:3000, url: http://localhost:3000
- info Loaded env from /Users/marcus-rise/WebstormProjects/My-Site/.env
- info (pwa) PWA support is disabled.
- info (pwa) PWA support is disabled.
- event compiled client and server successfully in 725 ms (20 modules)
- wait compiling...
- event compiled client and server successfully in 87 ms (20 modules)
- wait compiling /page (client and server)...
- event compiled client and server successfully in 898 ms (535 modules)
- error Error: Cannot find module 'webpack'
Require stack:
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/index.js
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/@ducanh2912/next-pwa/dist/index.cjs
- /Users/marcus-rise/WebstormProjects/My-Site/next.config.js
   at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
   at /Users/marcus-rise/WebstormProjects/My-Site/node_modules/next/dist/server/require-hook.js:185:36
   at Module._load (node:internal/modules/cjs/loader:920:27)
   at Module.require (node:internal/modules/cjs/loader:1141:19)
   at require (node:internal/modules/cjs/helpers:110:18)
   at Object.<anonymous> (/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js:18:35)
   at Module._compile (node:internal/modules/cjs/loader:1254:14)
   at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
   at Module.load (node:internal/modules/cjs/loader:1117:32)
   at Module._load (node:internal/modules/cjs/loader:958:12) {
 type: 'Error',
 code: 'MODULE_NOT_FOUND',
 requireStack: [
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js',
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/index.js',
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/@ducanh2912/next-pwa/dist/index.cjs',
   '/Users/marcus-rise/WebstormProjects/My-Site/next.config.js'
 ]
}
- wait compiling /_error (client and server)...
- event compiled client and server successfully in 226 ms (668 modules)
- wait compiling /favicon.ico/route (client and server)...
- event compiled client and server successfully in 136 ms (713 modules)
Error: Cannot find module 'webpack'
Require stack:
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/index.js
- /Users/marcus-rise/WebstormProjects/My-Site/node_modules/@ducanh2912/next-pwa/dist/index.cjs
- /Users/marcus-rise/WebstormProjects/My-Site/next.config.js
   at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
   at /Users/marcus-rise/WebstormProjects/My-Site/node_modules/next/dist/server/require-hook.js:185:36
   at Module._load (node:internal/modules/cjs/loader:920:27)
   at Module.require (node:internal/modules/cjs/loader:1141:19)
   at require (node:internal/modules/cjs/helpers:110:18)
   at Object.<anonymous> (/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js:18:35)
   at Module._compile (node:internal/modules/cjs/loader:1254:14)
   at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
   at Module.load (node:internal/modules/cjs/loader:1117:32)
   at Module._load (node:internal/modules/cjs/loader:958:12) {
 code: 'MODULE_NOT_FOUND',
 requireStack: [
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/generate-sw.js',
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/workbox-webpack-plugin/build/index.js',
   '/Users/marcus-rise/WebstormProjects/My-Site/node_modules/@ducanh2912/next-pwa/dist/index.cjs',
   '/Users/marcus-rise/WebstormProjects/My-Site/next.config.js'
 ]
}
Production successfull build logs on Vercel
[10:22:15.040] Running build in Cleveland, USA (East) – cle1
[10:22:15.101] Cloning github.com/Marcus-Rise/My-Site (Branch: chore/upgrade, Commit: 71c2b4a)
[10:22:15.500] Cloning completed: 398.989ms
[10:22:18.448] Restored build cache
[10:22:18.479] Running "vercel build"
[10:22:18.981] Vercel CLI 31.0.1
[10:22:19.246] Installing dependencies...
[10:22:19.556] yarn install v1.22.17
[10:22:19.625] [1/4] Resolving packages...
[10:22:19.853] [2/4] Fetching packages...
[10:22:35.974] [3/4] Linking dependencies...
[10:22:35.976] warning " > @ducanh2912/next-pwa@9.1.4" has unmet peer dependency "webpack@>=5.9.0".
[10:22:35.976] warning "@ducanh2912/next-pwa > clean-webpack-plugin@4.0.0" has unmet peer dependency "webpack@>=4.0.0 <6.0.0".
[10:22:35.976] warning "@ducanh2912/next-pwa > terser-webpack-plugin@5.3.9" has unmet peer dependency "webpack@^5.1.0".
[10:22:35.976] warning "@ducanh2912/next-pwa > workbox-webpack-plugin@7.0.0" has unmet peer dependency "webpack@^4.4.0 || ^5.9.0".
[10:22:35.981] warning " > @typescript-eslint/eslint-plugin@5.61.0" has unmet peer dependency "@typescript-eslint/parser@^5.0.0".
[10:22:46.999] [4/4] Building fresh packages...
[10:22:47.408] Done in 27.86s.
[10:22:47.434] Detected Next.js version: 13.4.9
[10:22:47.435] Running "yarn run build"
[10:22:47.635] yarn run v1.22.17
[10:22:47.667] $ next build
[10:22:48.022] - info Loaded env from /vercel/path0/.env
[10:22:48.765] - info Creating an optimized production build...
[10:22:49.275] - info (pwa) Compiling for client (static)...
[10:22:49.275] - info (pwa) Service worker: public/sw.js
[10:22:49.275] - info (pwa)   URL: /sw.js
[10:22:49.275] - info (pwa)   Scope: /
[10:22:49.308] - info (pwa) Compiling for server...
[10:22:49.311] - info (pwa) Compiling for server...
[10:23:05.467] - info Compiled successfully
[10:23:05.468] - info Linting and checking validity of types...
[10:23:07.580] - error ESLint: prettier.resolveConfig.sync is not a function Occurred while linting /vercel/path0/src/@types/index.d.ts:1 Rule: "prettier/prettier"
[10:23:08.469] - info Collecting page data...
[10:23:23.191] - info Generating static pages (0/10)
[10:23:23.255] - info Generating static pages (2/10)
[10:23:23.263] - info Generating static pages (4/10)
[10:23:23.275] - info Generating static pages (7/10)
[10:23:23.531] - info Generating static pages (10/10)
[10:23:23.547] - info Finalizing page optimization...
[10:23:23.550] 
[10:23:23.560] Route (app)                                Size     First Load JS
[10:23:23.560] ┌ ○ /                                      6.96 kB        86.7 kB
[10:23:23.560] ├ ○ /apple-icon.png                        0 B                0 B
[10:23:23.560] ├ ○ /favicon.ico                           0 B                0 B
[10:23:23.560] ├ ○ /icon.png                              0 B                0 B
[10:23:23.560] ├ ○ /manifest.webmanifest                  0 B                0 B
[10:23:23.561] ├ ○ /opengraph-image.jpg                   0 B                0 B
[10:23:23.561] ├ ○ /robots.txt                            0 B                0 B
[10:23:23.562] └ ○ /sitemap.xml                           0 B                0 B
[10:23:23.562] + First Load JS shared by all              79.8 kB
[10:23:23.562]   ├ chunks/839-7c7d2683d752739d.js         27.3 kB
[10:23:23.562]   ├ chunks/bce60fc1-a4c8f5ac12bef530.js    50.5 kB
[10:23:23.562]   ├ chunks/main-app-9558a928410b261a.js    222 B
[10:23:23.562]   └ chunks/webpack-355355f103a84797.js     1.71 kB
[10:23:23.562] 
[10:23:23.562] Route (pages)                              Size     First Load JS
[10:23:23.562] ─ ○ /404                                   181 B          77.7 kB
[10:23:23.562] + First Load JS shared by all              77.5 kB
[10:23:23.562]   ├ chunks/framework-8883d1e9be70c3da.js   45 kB
[10:23:23.562]   ├ chunks/main-e1e31ed019ac7d84.js        30.6 kB
[10:23:23.562]   ├ chunks/pages/_app-b75b9482ff6ea491.js  195 B
[10:23:23.562]   └ chunks/webpack-355355f103a84797.js     1.71 kB
[10:23:23.563] 
[10:23:23.564] ○  (Static)  automatically rendered as static HTML (uses no initial props)
[10:23:23.564] 
[10:23:24.240] Done in 36.61s.
[10:23:27.609] Traced Next.js server files in: 3.330s
[10:23:31.719] Created all serverless functions in: 4.108s
[10:23:31.726] Collected static files (public/, static/, .next/static): 4.917ms
[10:23:32.341] Build Completed in /vercel/output [1m]
[10:23:32.570] Deploying outputs...
[10:23:39.921] Deployment completed
[10:23:45.015] Uploading build cache [107.09 MB]...
[10:23:47.458] Build cache uploaded: 2.442s

@balazsorban44
Copy link
Member

balazsorban44 commented Jul 7, 2023

Hmm. Both of your reproductions worked for me with 13.4.9 @alifa20 and @Marcus-Rise. 🤔

What I can see is that both of you used next-pwa, so maybe it's related. (@alifa20 you are using a version that hasn't been updated for over a year though, I believe https://github.com/DuCanhGH/next-pwa is the currently maintained fork, updated 3 days ago)

Could you try to remove next-pwa from next.config.js so we can confirm whether it's related or not?

@alifa20 if you could provide the error stack, that would also be very helpful, thanks!

There are also only 4 releases between 13.4.8 and 13.4.9: 13.4.9-canary.0 - 13.4.9-canary.3, could you bisect the versions to find out exactly which one caused the issue for you? then we can try to find the exact PR from v13.4.8...v13.4.9

@luizeboli
Copy link

@balazsorban44 I'm also experiencing this error using the library https://github.com/dlehmhus/next-with-linaria. At least for me the error starts in release v13.4.9-canary.3.

v13.4.9-canary.2...v13.4.9-canary.3

@alifa20
Copy link
Author

alifa20 commented Jul 8, 2023

@balazsorban44 Thanks for the reply.

  1. So when running yarn dev, you'd need to load the localhost:3000 to see the error. The error comes up after loading the browser, and not just after running the yarn dev command.
➜  next-js-demo git:(update-next-js-13.4.9) ✗ yarn dev
yarn run v1.22.19
warning nextarter-chakra@0.1.0: The engine "pnpm" appears to be invalid.
$ next dev
- ready started server on 0.0.0.0:3000, url: http://localhost:3000
> [PWA] PWA support is disabled
> [PWA] PWA support is disabled
- event compiled client and server successfully in 206 ms (20 modules)
- wait compiling...
- event compiled client and server successfully in 78 ms (20 modules)
- wait compiling /page (client and server)...
- wait compiling /_error (client and server)...
- event compiled client and server successfully in 6.1s (2530 modules)
- warn Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
- error Error: Cannot find module 'webpack'
Require stack:
- /Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js
- /Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/index.js
- /Users/<my_mac_user>/my/next-js-demo/node_modules/next-pwa/index.js
- /Users/<my_mac_user>/my/next-js-demo/next.config.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /Users/<my_mac_user>/my/next-js-demo/node_modules/next/dist/server/require-hook.js:185:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js:18:35)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12) {
  type: 'Error',
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js',
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/index.js',
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/next-pwa/index.js',
    '/Users/<my_mac_user>/my/next-js-demo/next.config.js'
  ]
}
Error: Cannot find module 'webpack'
Require stack:
- /Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js
- /Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/index.js
- /Users/<my_mac_user>/my/next-js-demo/node_modules/next-pwa/index.js
- /Users/<my_mac_user>/my/next-js-demo/next.config.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at /Users/<my_mac_user>/my/next-js-demo/node_modules/next/dist/server/require-hook.js:185:36
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js:18:35)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/generate-sw.js',
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/workbox-webpack-plugin/build/index.js',
    '/Users/<my_mac_user>/my/next-js-demo/node_modules/next-pwa/index.js',
    '/Users/<my_mac_user>/my/next-js-demo/next.config.js'
  ]
}

I believe the stack error is very similar to what @Marcus-Rise has mentioned actually.

  1. Removing next-pwa from next.config.js seems to make the error go away. 👍
  2. I tried using @ducanh2912/next-pwa as you suggested, but still same error. ❌
  3. When I add webpack yarn add -D -E webpack it seems like it fixes the issue. You don't see the error, it might be because you have installed webpack globally? ¯\(ツ)/¯ 🤷‍♂️
  4. I have updated the pr for the changes we mentioned above: https://github.com/alifa20/next-js-demo/pull/1/commits
  5. After checking the code changes in next repo: https://github.com/vercel/next.js/compare/v13.4.8%E2%80%A6v13.4.9#diff-5f0a6c7f8a0c0b2d7e6c0f5a8e5b7a9b6d8b3f9d3e7c2a2d5f0c9a5b8e1c6f6
    seems like one of the PRs have made the project need to have webpack installed separately. My guess is this pr https://github.com/vercel/next.js/pull/52186/files#diff-5f0a6c7f8a0c0b2d7e6c0f5a8e5b7a9b6d8b3f9d3e7c2a2d5f0c9a5b8e1c6f6R54
    PR link: update turbopack #52186

@balazsorban44
Copy link
Member

balazsorban44 commented Jul 8, 2023

1 First thing I did, still worked
2-4 thanks!
5-7 no, next comes with webpack prebundled, so you should definitely not need to install it yourself. This sounds like a bug, will look, more into it. (@luizeboli thats true for you too, webpack should not be added as a dependency)

@alifa20
Copy link
Author

alifa20 commented Jul 8, 2023

@balazsorban44 Really appreciate the investigation thanks 🙏

@balazsorban44
Copy link
Member

balazsorban44 commented Jul 8, 2023

I'm leaning towards #52241 being related.

@limegorilla
Copy link

Will add a +1 to this being related to next-pwa - removed that from my config and all was good

@ekwoka
Copy link

ekwoka commented Jul 11, 2023

next-pwa isn't in our config and this is happening regardless.

I can also see that webpack isn't being installed....

there is a watchpack

@Itsnotaka
Copy link

Commented out import of next-pwa (Both the maintained and unmaintained version) works

@shuding shuding self-assigned this Jul 12, 2023
@shuding shuding added the linear: next Confirmed issue that is tracked by the Next.js team. label Jul 12, 2023
@kodiakhq kodiakhq bot closed this as completed in #52587 Jul 12, 2023
kodiakhq bot pushed a commit that referenced this issue Jul 12, 2023
This PR ensures that both Webpack and the config won't be initiated in render workers. This is great for performance but also avoids potential issues (e.g. Next.js plugin with side effects). Instead, we pass a serialized config from the router worker to the render workers.

Closes #52366.
@DuCanhGH
Copy link
Contributor

DuCanhGH commented Jul 12, 2023

@alifa20 can you try my fork's 9.2.1? Thanks!

I moved webpack to "dependencies"; which means that if you install my fork, webpack will also be automatically installed. This is needed by next-pwa as:

  • We rely on webpack to build the custom worker, the fallback worker and more.
  • workbox-webpack-plugin imports webpack and I don't think there is a way to force it to change to next/dist/compiled/webpack.
  • next/dist/compiled/webpack is an internal module and most likely is not present in some older Next versions, which means that we would need to use dynamic require with try catch, so even if we could alias webpack to next/dist/compiled/webpack it might not work anyway.

We also rely on certain webpack plugins that list it as a dependency so it is installed anyway.

Edit: seems that this is a Next.js issue, but this change also fixes this issue anyway :D

@alifa20
Copy link
Author

alifa20 commented Jul 13, 2023

@DuCanhGH Your suggested version definitely fixed the issue with existing next.js v13.4.9 .

Thanks for jumping in and updating this issue thread 🙏

Also thanks to @shuding that his PR seems to fix the issue for v13.4.10-canary.5
Also the latest version of both libraries work together as well.

Link to the commit of the fix next-pwa 9.2.1, next.js v13.4.9
Link to the commit of the fix next-pwa ^9.1.4, next.js v13.4.10-canary.5
Link to the commit of the fix next-pwa 9.2.1, next.js v13.4.10-canary.5

@alifa20
Copy link
Author

alifa20 commented Jul 13, 2023

That being said, @DuCanhGH you might wanna move the webpack back into devDependancy? Since the new fix doesn't need webpack to be installed, as it was before.
Up to you 👍

@DuCanhGH
Copy link
Contributor

@alifa20 I will wait for 13.4.10 to release first, then revert later with a note in next-pwa's docs :)

@alifa20
Copy link
Author

alifa20 commented Jul 13, 2023

@DuCanhGH
Perfect
Good stuff 👍

@jkiss
Copy link

jkiss commented Jul 13, 2023

I'm not using next-pwa, but also get this error, it's really a Next.js 13.4.9 issue, fallback to 13.4.7 😅

@Neo-Ciber94
Copy link

Neo-Ciber94 commented Jul 15, 2023

I'm using NextJS 13.4.9 in my case I just yarn add -D webpack webpack as a devDependency.

@shuding
Copy link
Member

shuding commented Jul 27, 2023

@jkiss @Neo-Ciber94 Please read the above messages, this was fixed in versions newer than 13.4.9. Thanks!

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked
Projects
None yet
Development

Successfully merging a pull request may close this issue.