Don’t Track Node Modules Folder In Your Project

GENERAL NO NO

During the early days of my web development career, I had difficulty figuring out why my project folder was so bloated regarding file size. Passing the files to a thumb drive or delivering it through a file uploader, took forever. I later realized the unnecessary need to include the node_modules folder. This is especially apparent when you are developing apps. However, it didn’t occur to me that I needed to make sure when building websites, I should do the same, especially with new themes in WordPress.

WHAT ARE THEY

What is this bloating folder you wonder? Well, it’s a directory of folders and script files—mostly task-oriented programs. For example, if one used SASS or Gulp commands to compile CSS files, the JavaScript functions would run from the node_modules folder when you type in a command from your terminal. However, once the code is compiled, you don’t need them in production.

STRATEGY

Okay, so what to do when you need to work with dependencies, but don’t want to keep them in the repo? Well, we need to remember to put them in the git ignore file. You simply generate this file by adding “.gitignore” in the root directory of your project. In my case for WordPress theme development, you add it within the same folder as the custom theme itself. This way the computer will know what not to include when pushing the files up to a repo for storage.

Within this folder, you want to specify the directory that holds the node_modules and isolate it by typing “/node_modules” on a new line. Be sure to leave a comment as to what you’re ignoring up above.

There are a lot of other things to cover in the .gitignore file. Things that are generated from an Apple or Windows computer, folders of images, and experimental scripts you don’t want to be uploaded to your git repo. I’ll most likely expand on this topic in a future blog post. But for now, this was something I needed to jot down for safekeeping.