DEV Community

Cover image for Handling node_modules Being push to repository in Node powered projects.
ADEOYE ADEFEMI OPEOLUWA
ADEOYE ADEFEMI OPEOLUWA

Posted on

Handling node_modules Being push to repository in Node powered projects.

In my experience, working with mono-repo projects, I almost often end up adding node_modules to my remote repo 😩.

I finally found a way of working around it, hope to share. It's gonna be brief. But should you need to grab a cup of coffee, now is a good time to do that.

Let's Get Started

A typical mono-repo project I've worked with has a following folder structure similar to this
folder structure

With each folder having it own package.json file and node_modules folder
detailed structure of mono-repo

The pitfall

A common mistake is running

git add .
Enter fullscreen mode Exit fullscreen mode


` in any of the folder other than the root directory, this add all content of the folder (and node_modules too 😕) to the git history.

Way out

I found out adding .gitignore to each folder (api, client in this case) with the following content
`

./node_modules
Enter fullscreen mode Exit fullscreen mode



and

./api/node_modules
./client/node_modules
Enter fullscreen mode Exit fullscreen mode


in the root folder will make sure node_modules is not added to the remote repository even if you run

git add .
Enter fullscreen mode Exit fullscreen mode


`
in any of the folders having node_modules

fixing the error

Top comments (0)