Bird
Raised Fist0
NextJSframework~20 mins

Server-only modules in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Server-only Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Next.js server component importing a server-only module?

Consider a Next.js server component that imports a server-only module to fetch data. What will be rendered on the client?

NextJS
import getData from './server-only-module';

export default async function Page() {
  const data = await getData();
  return <div>{data.message}</div>;
}

// server-only-module.js
import 'server-only';
export default async function getData() {
  return { message: 'Hello from server!' };
}
A<div>Hello from server!</div>
B<div>[object Promise]</div>
CError: Module not found on client
D<div>undefined</div>
Attempts:
2 left
💡 Hint

Think about where server-only modules run and how data is fetched before rendering.

📝 Syntax
intermediate
1:30remaining
Which import statement correctly imports a server-only module in Next.js?

Given a server-only module at lib/server-utils.js, which import statement is valid in a server component?

Aimport utils from 'lib/server-utils';
Bimport utils from './server-utils';
Cimport utils from '../lib/server-utils';
Dimport utils from '/lib/server-utils';
Attempts:
2 left
💡 Hint

Consider relative paths and module resolution in Next.js.

🔧 Debug
advanced
2:30remaining
Why does this Next.js server component fail to compile when importing a server-only module in a client component?

Examine the code below. Why does it cause a build error?

NextJS
// client-component.jsx
'use client';
import secret from '../lib/server-secret';

export default function ClientComponent() {
  return <div>{secret}</div>;
}

// lib/server-secret.js
import 'server-only';
const secret = 'top-secret';
export default secret;
AClient components cannot import server-only modules, causing a build error.
BThe 'use client' directive is missing in the server-secret module.
CThe import path '../lib/server-secret' is invalid.
DThe secret variable is not exported correctly.
Attempts:
2 left
💡 Hint

Think about the difference between client and server components and module usage.

❓ state_output
advanced
2:00remaining
What is the value of 'result' after running this Next.js server component code?

Given the server-only module and server component below, what is the value of result?

NextJS
// server-only-module.js
import 'server-only';
export function compute() {
  return 42;
}

// page.jsx
import { compute } from './server-only-module';

export default function Page() {
  const result = compute();
  return <div>{result}</div>;
}
Aundefined
B42
CError: compute is not a function
Dnull
Attempts:
2 left
💡 Hint

Consider how server-only modules export functions and how they are used in server components.

🧠 Conceptual
expert
3:00remaining
Which statement best describes the behavior of server-only modules in Next.js?

Choose the most accurate description of server-only modules in Next.js.

AServer-only modules are deprecated and replaced by client components in Next.js.
BServer-only modules can be imported by both client and server components without restrictions.
CServer-only modules run on the client but fetch data from the server asynchronously.
DServer-only modules run exclusively on the server and cannot be imported by client components.
Attempts:
2 left
💡 Hint

Think about where server-only modules execute and their import limitations.

Practice

(1/5)
1. What is the main purpose of server-only modules in Next.js?
easy
A. To enable server code to be bundled with client JavaScript
B. To run code on both server and client for faster rendering
C. To allow client-side code to access server secrets directly
D. To run code only on the server and keep it hidden from the browser

Solution

  1. Step 1: Understand server-only module purpose

    Server-only modules run exclusively on the server and are not sent to the browser.
  2. Step 2: Identify security benefit

    This keeps secrets safe and improves app security by hiding sensitive code from clients.
  3. Final Answer:

    To run code only on the server and keep it hidden from the browser -> Option D
  4. Quick Check:

    Server-only modules hide code from browser = A [OK]
Hint: Server-only means code runs only on server, hidden from browser [OK]
Common Mistakes:
  • Thinking server-only modules run on client too
  • Assuming server-only modules expose secrets to browser
  • Confusing server-only with shared server-client code
2. Which line correctly marks a Next.js module as server-only?
easy
A. export const client = true;
B. export const runtime = 'edge';
C. import 'server-only';
D. export default function ServerOnly() {}

Solution

  1. Step 1: Recall server-only marking syntax

    Next.js uses import 'server-only'; to mark server-only modules.
  2. Step 2: Compare options

    import 'server-only'; matches the correct syntax; others either mark edge runtime or client code.
  3. Final Answer:

    import 'server-only'; -> Option C
  4. Quick Check:

    Server-only marker = import 'server-only' [OK]
Hint: Look for import 'server-only' to mark server-only [OK]
Common Mistakes:
  • Using 'edge' runtime which is for edge functions, not server-only
  • Confusing client export with server-only marking
  • Missing the import keyword
3. Given this server-only module code in Next.js, what will happen if you try to import it in a client component?
import 'server-only';

export function secretData() {
  return 'top-secret';
}
medium
A. The client component will fail to compile with an error
B. The client component will receive the secret data as a string
C. The secretData function will run on the client without errors
D. The secretData function will be bundled into client JavaScript

Solution

  1. Step 1: Understand server-only import restrictions

    Server-only modules cannot be imported into client components; Next.js blocks this to protect secrets.
  2. Step 2: Predict behavior on import

    Trying to import causes a compile-time error because the module is not allowed in client code.
  3. Final Answer:

    The client component will fail to compile with an error -> Option A
  4. Quick Check:

    Importing server-only in client causes error = D [OK]
Hint: Server-only modules cannot be imported by client components [OK]
Common Mistakes:
  • Assuming server-only code runs on client
  • Thinking secret data is exposed to client
  • Believing server-only modules are bundled with client code
4. You wrote this server-only module but get an error when running your Next.js app:
export function getSecret() {
  return 'hidden';
}

What is missing to make this module server-only?
medium
A. You need to add import 'server-only'; at the top
B. You must rename the function to start with 'server'
C. You should import React in this module
D. You need to export the function as default

Solution

  1. Step 1: Identify server-only module requirement

    Next.js requires import 'server-only'; to mark a module as server-only.
  2. Step 2: Check given code

    The code lacks this import, so Next.js treats it as shared or client code, causing errors if used improperly.
  3. Final Answer:

    You need to add import 'server-only'; at the top -> Option A
  4. Quick Check:

    Missing 'server-only' import causes error = A [OK]
Hint: Always add import 'server-only'; to mark server-only [OK]
Common Mistakes:
  • Thinking function name controls server-only status
  • Assuming React import is needed for server-only modules
  • Believing default export affects server-only behavior
5. You want to fetch a secret API key only on the server in Next.js and prevent it from being sent to the client. Which approach correctly uses server-only modules to achieve this?
// secret.js
import 'server-only';
export function getApiKey() {
  return process.env.API_KEY;
}

// page.jsx
'use client';
import { getApiKey } from './secret';

export default function Page() {
  const key = getApiKey();
  return <p>API Key: {key}</p>;
}
hard
A. This code will cause an error because getApiKey is called in a client component
B. You should move getApiKey call inside a server component or server action
C. This code works and safely shows the API key on the client
D. You must export getApiKey as default to work

Solution

  1. Step 1: Understand server-only module usage

    Server-only modules cannot be imported or called directly in client components without error.
  2. Step 2: Analyze the code

    The page.jsx is a client component calling getApiKey(), which is server-only, causing an error.
  3. Step 3: Correct approach

    Call getApiKey() inside a server component or server action to keep the secret on the server.
  4. Final Answer:

    You should move getApiKey call inside a server component or server action -> Option B
  5. Quick Check:

    Server-only calls must be inside server components = B [OK]
Hint: Call server-only functions only inside server components or actions [OK]
Common Mistakes:
  • Calling server-only functions directly in client components
  • Exposing secrets by rendering them in client JSX
  • Assuming default export fixes server-only usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard