Open In App

Folder structure for a Node JS project

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Organizing a Node JS project with well planned folder structure is crucial for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of the Node JS project. We are going to learn all the directories present in the folder structure and files present in the Node JS project.

Prerequisites

To understand the folder structure for a Node JS project, ensure you have:

  • Basic knowledge of Node JS and npm
  • A code editor or IDE
  • Node JS installed on your machine

Folder Structure Overview:

node-folder-format

Node project folder structure

Folder structure contains the files and folders that are present in the directory. There are multiple files and folders, for example, api, config, models, services, subscribers, app.js, server.js, package-lock.json, package.json, readme.md, etc.

Steps to create Folder Structure

Step 1: Open the terminal and go to the path where you want to create the project and create a folder with your project name.

mkdir folder_name

Step 2: Run the command npm init to initialized the node project. This will create the package.json file

npm init

Step 3: Install the dependencies like express, nodemon etc. This will create package-lock.json file and node_modules folder.

npm i express nodemon

Step 4: Run the command git-init to initialized the git in the project. This will add .gitignore file.

git init

Step 5: Create a file named Readme.md which will contain all the info of the project.

touch Readme.md

Step 6: Create a file with extension .env which will contain the sensitive information and credentials of the project.

touch process.env

Step 7: Create a file named app.js or index.js will will be the end point to run the application.

touch app.js

Step 8: Create folder like public (contains static files and resources) and src (contains folders like controllers, models routes, views).

mkdir src

Files and folders details

  • Root Directory: The root directory is the main folder in which all the files and folders for that particular application are present, like the entrpoint file, src folder, public folder, routes, models, views, and controllers.
  • Node Modules: This folder contains the files and folders regarding installed files and dependencies that are used in this project. All the installation files for dependencies are stored in node modules.
  • Public: This folder contains static files that are visible to people, like index.html, script.js, and CDN links and files.
  • Source: This folder present with the name of src which contains all the files required to handle server like routes, controllers, models, views, etc.
  • Routes: This will contains all the routes and endpoints required for the server and also thier required files like for authentication routes we can create a file and setup routes.
  • Controllers: Controllers folder contains the business logic and validations for the input data received by the client side and performs their business logic and sends it to database controllers, which contains logical files and folders.
  • Models: Models contain all the schemas of the database, like which kind of input will be received from client-side and server-side validations. This will contain all files of validations and data schemas that should exist.
  • Package.json: This file contains the data and details of all dependencies installed in your project. Express is a kind of dependency that is used to establish a server, so this file will contain all the details regarding Express, like version and installed.
  • App.js: It is the entrypoint file of the server, which contains the main routes of the application and server ports from which the server will start listening, as well as the basic routes used in this application.
  • .gitignore: Git ignore is a file that contains files and folders that should not be pushed to the server. The git ignore file is used to stop pushing files from the server, like node module files, which should not be pushed to the server because they can easily be installed with the package.json file.
  • Readme.md: This is a markdown file where we write down information about your project, like all development details about your application.
  • env: The env file stands for Environmental Variables of Application. This file contains details about environment variables like API keys, the private salt of a Payment Gateway, and many other details that should be kept private.

Why Node JS project structure is important?

Project structure shows the organization of code and files, and a clean, simple, and decent project structure shows a clean written code, which helps to debug code, and another developer can easily read it. Also, while deploying code on the server, it can recognize files easily, which is why developers are implementing clean, simple, and decent project structures.

Best Practices for Node JS Project Structure

It is best practice to create a project structure for the NODE JS application, separate files according to their work, and separate them in the correct directories. For example, business logic should be present in the controllers folder, and also standardize the naming conventions of the application so that it will be easy to verify the logic and presence of any particular file or folder.

Conclusion:

Creating a well-organized folder structure is important for maintaining a Node JS project. It increases readability, maintainability, and scalability of the application. By adopting a well structure, it will be easy to manage code.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads