When working on Node.js projects, it’s common to have a large number of dependencies installed in the
node_modules folder. This folder can be quite large and can slow down the syncing process when backing up or transferring your project to Dropbox.
To overcome this issue, you can use rsync, a powerful command-line tool for synchronizing files and directories, along with a bash script that excludes the
node_modules folder and also filter out anything in a
.gitignore file that you specify. In this article, I’ll guide you through the process of setting up and using this bash script to sync your Node.js project while ignoring the
# Step 1: Create the Bash Script
To get started, open your favorite text editor and create a new file. You can name it something like
sync-node-projects.sh. Paste the following code into the file:
Let’s go through the script and understand what each part does:
- The first line (
#!/usr/bin/env bash) specifies the interpreter for the script as bash.
set -ecommand ensures that the script exits immediately if any command within it returns a non-zero status, indicating an error.
src_dirvariable defines the source directory path, where your Node.js project is located. Update it with the appropriate path for your system.
dest_dirvariable defines the destination directory path, where you want to backup or transfer your project. Modify it to suit your needs.
rsynccommand is responsible for synchronizing the files and directories. Let’s examine its options:
-aflag stands for “archive mode” and is a convenient way to sync files while preserving their permissions, ownership, and other attributes.
--deleteoption ensures that any files or directories that exist in the destination but not in the source are deleted during the sync. This helps keep both locations identical.
--filter=':- .gitignore'flag excludes files and directories specified in the
.gitignorefile from being synced. This ensures that files ignored by Git are also ignored by rsync.
--exclude='node_modules'option tells rsync to skip syncing the
node_modulesfolder, which can be large and unnecessary for backup or transfer purposes.
--exclude='.git'flag excludes the
.gitfolder, as it is not required for syncing and can be quite large.
--exclude='.DS_Store'option excludes the
.DS_Storefile, which is specific to macOS and contains metadata that is not relevant for syncing.
--chmod='F-w'flag removes write permissions from all files in the destination directory, ensuring they remain read-only. This prevents accidental modifications in the backup location.
# Step 2: Save and Make the Script Executable
After pasting the script into your text editor, save the file and navigate to the location where you saved it using the terminal. To make the script executable, run the following command:
This command grants execute permissions to the script.
# Step 3: Run the Script
To sync your Node.js project, open a terminal and navigate to the location of the script. Execute the script by running the following command:
The script will start syncing your Node.js project from the source directory (
src_dir) to the destination directory (
dest_dir), excluding the
Congratulations! You have successfully set up a bash script that uses rsync to sync your Node.js project while ignoring the
node_modules folder. By excluding this folder from the syncing process, you can save time and space, especially when working with large projects. Remember to run the script whenever you want to sync your project, and all your changes will be transferred while keeping the unnecessary
node_modules folder out of the equation.