0
0
NextjsHow-ToBeginner · 3 min read

How to Use Output Standalone in Next.js for Independent Builds

In Next.js, you can enable output: 'standalone' in your next.config.js to create a self-contained build folder that includes all necessary files to run your app independently. This is useful for deploying your app without needing to install dependencies separately.
📐

Syntax

To use the standalone output in Next.js, add the output: 'standalone' option inside the next.config.js file's export object. This tells Next.js to bundle your app and its dependencies into a single folder.

  • output: The key to specify the build output type.
  • 'standalone': The value that creates a self-contained build folder.
javascript
module.exports = {
  output: 'standalone',
};
💻

Example

This example shows a minimal next.config.js with standalone output enabled. After running next build, Next.js creates a .next/standalone folder containing your app and all dependencies. You can then run your app from this folder without installing node_modules.

javascript
/* next.config.js */
module.exports = {
  output: 'standalone',
};
Output
Build completed. Standalone output created in .next/standalone folder.
⚠️

Common Pitfalls

Common mistakes when using output: 'standalone' include:

  • Not running next build after setting the option, so the standalone folder is not created.
  • Trying to run the app from the root folder instead of the .next/standalone folder.
  • Forgetting to copy the package.json and next.config.js files if deploying the standalone folder elsewhere.

Always run node server.js inside the standalone folder to start your app.

bash
/* Wrong: Running app from root without standalone build */
// This will fail if node_modules are missing
node server.js

/* Right: Run inside standalone folder */
cd .next/standalone
node server.js
📊

Quick Reference

OptionDescription
output: 'standalone'Creates a self-contained build folder with all dependencies.
next buildGenerates the standalone folder after setting the option.
.next/standaloneFolder containing the standalone app ready to run.
node server.jsCommand to start the app inside the standalone folder.

Key Takeaways

Set output: 'standalone' in next.config.js to create a self-contained build.
Run next build to generate the standalone folder with all dependencies.
Run your app from the .next/standalone folder using node server.js.
Standalone output helps deploy apps without installing node_modules separately.
Remember to copy necessary config files if deploying the standalone folder elsewhere.