serverComponentsExternalPackages on Nextjs breaks production builds
I'm facing an issue with Ably integration in Next.js App Router where I need serverComponentsExternalPackages: ["ably"] to stop this dev mode warning, related to this issue
⚠ ./node_modules/keyv/src/index.js
Critical dependency: the request of a dependency is an expression
Import trace for requested module:
./node_modules/keyv/src/index.js
./node_modules/cacheable-request/src/index.js
./node_modules/got/dist/source/core/index.js
./node_modules/got/dist/source/create.js
./node_modules/got/dist/source/index.js
./node_modules/ably/build/ably-node.js
and while adding serverComponentsExternalPackages: ["ably"] to next.config does solve these warnings I get another dev mode errors (non-breaking), and build errors (breaking)
-
dev error (non-breaking):
-
build error (breaking):
Current Implementation:
- Using dynamic imports with
ssr: falsefor Ably components - Getting Ably client from API route with authUrl: "/api"
Versions:
- Next.js: 14.2.29
- Ably: 2.9.0
Hey @Abdellah-Idrissi, thank you for reporting the issue. From the error message, it seems that your app can’t resolve some of the imports. Could you provide a minimal reproducible example so we can take a closer look at the problem? You can also try updating Next to 15.x. The keyv package should be automatically opted out of bundling in the latest version of Next.js.
@ttypic Thanks for the response!
Minimal repo: repo - reproduces the exact issues described Next.js 15: Unfortunately we can't upgrade to Next 15 currently due to project constraints, so we're stuck on 14.x.
Thank you, @Abdellah-Idrissi. We’ll take a look and get back to you soon
Well??