Implementation:Langgenius Dify Copy And Start
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Build_Tools |
| Last Updated | 2026-02-12 07:00 GMT |
Overview
Production startup script that copies required static assets into the Next.js standalone output directory and then launches the production server.
Description
copy-and-start.mjs is a Node.js ESM script used as a replacement for next start in production deployments that use Next.js standalone output mode. The Next.js standalone build does not include the .next/static and public directories, so this script copies them into the standalone directory before starting the server.
The script performs two phases:
- Asset Copy Phase -- Iterates over a configured list of directory pairs and copies each source directory into the standalone output tree:
.next/staticis copied to.next/standalone/.next/staticpublicis copied to.next/standalone/public
- Parent directories are created automatically. If a required source directory does not exist, the script exits with code 1.
- Server Start Phase -- Spawns the Node.js process running
.next/standalone/server.jswith:- PORT from
npm_config_port,PORTenvironment variable, or defaults to3000 - HOSTNAME from
npm_config_host,HOSTNAMEenvironment variable, or defaults to0.0.0.0
- The server process inherits stdio and the script propagates its exit code.
- PORT from
Usage
Use this script in Docker containers or production environments where Next.js standalone output is deployed. It replaces the standard next start command to handle the asset copying that standalone mode requires.
Code Reference
Source Location
- Repository: Langgenius_Dify
- File: web/scripts/copy-and-start.mjs
- Lines: 1-115
Signature
const DIRS_TO_COPY = [
{ src: '.next/static', dest: '.next/standalone/.next/static' },
{ src: 'public', dest: '.next/standalone/public' },
]
const SERVER_SCRIPT_PATH = '.next/standalone/server.js'
const pathExists = async (path) => { ... }
const copyDir = async (src, dest) => { ... }
const copyAllDirs = async () => { ... }
const main = async () => { ... }
Import
// CLI script - run directly
node web/scripts/copy-and-start.mjs
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| .next/static | directory | Yes | Next.js compiled static assets (CSS, JS chunks, media) produced by next build
|
| public | directory | Yes | Public static files (favicon, images, robots.txt, etc.) |
| .next/standalone/server.js | file | Yes | The standalone server entry point produced by next build with output: 'standalone'
|
| PORT | env variable | No | Server port; defaults to 3000 |
| HOSTNAME | env variable | No | Server hostname; defaults to 0.0.0.0 |
Outputs
| Name | Type | Description |
|---|---|---|
| .next/standalone/.next/static | directory | Copied static assets inside standalone directory |
| .next/standalone/public | directory | Copied public assets inside standalone directory |
| Running server process | process | Node.js server listening on the configured host and port |
| exit code 0 | number | Server exited normally |
| exit code 1 | number | Missing build artifacts or server startup failure |
Usage Examples
Start production server (default port 3000)
node web/scripts/copy-and-start.mjs
Start with custom port and host
PORT=8080 HOSTNAME=127.0.0.1 node web/scripts/copy-and-start.mjs
Use in Dockerfile
CMD ["node", "scripts/copy-and-start.mjs"]