N
Nuxt3w ago
Surreal

Rollup error when using server components

I'm hitting a Rollup error when using server-only components, and I'm not sure how to best resolve it. Essentially, I'm using the Nuxt 4-style folder structures and have a server-only component, /app/components/MyComponent.server.vue, which is importing some server-only code, /server/db/UserRepository (essentially some code to connect to my database and retrieve a user). This works fine locally in development, but is throwing a Rollup error when I go to build the code for production. I'm pretty sure that the error is because Rollup is trying to include server-only libraries (i.e., postgres, which uses Node.js modules like perf_hook, etc.) in the client-bundle, but I'm not clear why it would be trying to include code imported by a server component. Is this a bug? A missing feature (i.e., don't try to bundle server component code in the browser bundle)? Any ideas how to workaround it? I'd rather not have to create an API endpoint for fetching this data, as it's not something I intend to expose externally.
3 Replies
kapa.ai
kapa.ai3w ago
Hi @Surreal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, it appears that you're encountering an issue related to server components and build errors in Nuxt. While there isn't a direct solution to your specific problem in the given sources, I can provide some insights and potential workarounds based on related information. 1. Server Components: Nuxt supports server components, which are meant to be rendered only on the server side. These components use the .server.vue suffix, as you've correctly implemented Server Components. 2. Build Errors: The Rollup error you're experiencing during production build is likely related to how Nuxt and its build process handle server-only code. 3. Potential Workaround: One possible solution is to use the build.transpile configuration to tell Nuxt not to try importing certain libraries. You could try adding your server-only libraries (like postgres) to this list:
export default defineNuxtConfig({
build: {
transpile: ['postgres']
}
})

export default defineNuxtConfig({
build: {
transpile: ['postgres']
}
})

This approach is suggested in the Troubleshooting ESM Issues documentation. 4. Vite Configuration: Another potential solution could be to extend the Vite configuration to handle server-only code differently. You could use the extendViteConfig function to modify how Vite handles certain imports:
import { defineNuxtModule, extendViteConfig } from '@nuxt/kit'

export default defineNuxtModule({
setup() {
extendViteConfig((config) => {
// Add custom Vite configuration here
})
}
})

import { defineNuxtModule, extendViteConfig } from '@nuxt/kit'

export default defineNuxtModule({
setup() {
extendViteConfig((config) => {
// Add custom Vite configuration here
})
}
})

This approach is based on the extendViteConfig documentation. 5. Reporting the Issue: If you believe this is a bug, you might want to report it to the Nuxt team. The Reporting Bugs guide provides steps on how to effectively report issues, including creating a minimal reproduction of the problem. While these suggestions might help, they are not guaranteed solutions to your specific problem. The issue you're facing seems to be a complex interaction between server components, build processes, and external libraries, which isn't directly addressed in the provided knowledge sources. If these workarounds don't resolve the issue, you may need to seek further assistance from the Nuxt community or consider reporting it as a potential bug. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3w ago
<@390675691667980293> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Surreal
SurrealOP3w ago
The specific error I get is:
[nuxi 1:12:15 PM] ERROR Nuxt Build Error: node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js (5:9): "performance" is not exported by "__vite-browser-external", imported by "node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js".

file: node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:5:9

3: import crypto from 'crypto'
4: import Stream from 'stream'
5: import { performance } from 'perf_hooks'
^
6:
7: import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'

at getRollupError (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:397:41)
at error (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:393:42)
at Module.error (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:16607:16)
at Module.traceVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:17055:29)
at ModuleScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:14718:39)
at FunctionScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionBodyScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionBodyScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at MemberExpression.bind (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:7282:49)
[nuxi 1:12:15 PM] ERROR Nuxt Build Error: node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js (5:9): "performance" is not exported by "__vite-browser-external", imported by "node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js".

file: node_modules/.pnpm/[email protected]/node_modules/postgres/src/connection.js:5:9

3: import crypto from 'crypto'
4: import Stream from 'stream'
5: import { performance } from 'perf_hooks'
^
6:
7: import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'

at getRollupError (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:397:41)
at error (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/parseAst.js:393:42)
at Module.error (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:16607:16)
at Module.traceVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:17055:29)
at ModuleScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:14718:39)
at FunctionScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionBodyScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at FunctionBodyScope.findVariable (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:5579:38)
at MemberExpression.bind (node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:7282:49)
I'm guessing it's possible to hack together a dummy polyfill for the Node.js dependencies in postgres, but what I'm confused by is why does Rollup even try to bundle server-only code imports into the client-side/browser bundle?

Did you find this page helpful?