How I Combined All Controllers, Routes, Models & Utilities into Organized Single Files Using Node.js

Hi, 👋 I'm Vinay Patel I am a Software Developer with a passion for building scalable and high-performance applications.
In most Node.js backend projects, especially when they start to grow, you end up with multiple folders like:
controllers/routes/models/middlewares/services/
Each folder contains dozens of .js files.
There are situations where you might want to combine all files from each folder into a single .txt document, such as:
Migrating to a new project
Sharing code with someone without exposing your full repository
Creating documentation or team references
Backing up readable versions of your code
Auditing your code structure quickly
Today I'm sharing a reusable Node.js script that automatically merges all .js files within these folders into separate output text files - in one single run.
Project Folder Structure (Example)
controllers/
routes/
models/
middlewares/
services/
└── utils/
combineAll.js ← our script
The Goal
We want the script to output:
controllers.txt
routes.txt
models.txt
middlewares.txt
services.txt
utils.txt
Each file should contain the merged content of all .js files inside its respective folder, with proper headers like:
/* ===== File: userController.js ===== */
Full Script: combineAll.js (Ready to Use)
This script works with ES Modules, so it supports import instead of CommonJS require.
Just place this file in the root of your backend project:
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
// Recreate __dirname for ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// ===== Generic function to combine .js files inside a folder =====
function combineFiles(folderPath, outputName) {
const targetDir = path.join(__dirname, folderPath);
const outputFile = path.join(__dirname, outputName);
if (!fs.existsSync(targetDir)) {
console.log(`Folder "${folderPath}" not found. Skipping...`);
return;
}
const files = fs
.readdirSync(targetDir)
.filter((file) => file.endsWith(".js"));
let combinedContent = "";
files.forEach((file) => {
const filePath = path.join(targetDir, file);
const content = fs.readFileSync(filePath, "utf8");
combinedContent += `\n\n/* ===== File: ${file} ===== */\n\n`;
combinedContent += content;
});
fs.writeFileSync(outputFile, combinedContent, "utf8");
console.log(`Combined ${folderPath} → ${outputName}`);
}
// ===== Run for all folders =====
// Main folders
combineFiles("controllers", "controllers.txt");
combineFiles("routes", "routes.txt");
combineFiles("models", "models.txt");
combineFiles("middlewares", "middlewares.txt");
// Services (main folder)
combineFiles("services", "services.txt");
// Services/utils subfolder
combineFiles("services/utils", "utils.txt");
console.log("\n All folders combined successfully!\n");
How to Run the Script
Open your terminal in the backend root:
node combineAll.js
This will instantly generate:
controllers.txtroutes.txtmodels.txtmiddlewares.txtservices.txtutils.txt
Each file neatly organizes your entire folder’s code for quick reference or export.
MY Final Thoughts
This simple script has saved my a lot of time when sharing, documenting, and migrating backend logic across systems. Instead of manually copying files, a single command neatly organizes and exports everything you need.
If your Node.js backend has grown large and complex, tools like this help bring clarity and speed back into your workflow.



