Next.js: How to mark a JavaScript module as server only
13 June 2025 (Updated 13 June 2025)
Use the server-only
package. For example:
import 'server-only'
export async function getData() {
const res = await fetch('https://external-service.com/data', {
headers: {
authorization: process.env.API_KEY,
},
})
return res.json()
}
Now, if you try to use that module in a client component, you should get a build error.
Tagged:
Next.js recipes