Question

[Solved] Git – Ignore node_modules folder everywhere

I have a project containing multiple other projects :

  • Main project
    • Mini project 1
    • Mini project 2

All containing node_modules folder. I want git to ignore the folder no matter where it is starting from the root folder. Something like this to add in .gitignore :

*node_modules/*
Enquirer: Mehdiway

||

Solution #1:

Add this

node_modules/

to .gitignore file to ignore all directories called node_modules in current folder and any subfolders

Respondent: Alik

Solution #2:

Use the universal one-liner in terminal in the project directory:

touch .gitignore && echo “node_modules/” >> .gitignore && git rm -r –cached node_modules ; git status

It works no matter if you’ve created a .gitignore or not, no matter if you’ve added node_modules to git tracking or not.

Then commit and push the .gitignore changes.

Explanation

touch will generate the .gitignore file if it doesn’t already exist.

echo and >> will append node_modules/ at the end of .gitignore, causing the node_modules folder and all subfolders to be ignored.

git rm -r --cached removes the node_modules folder from git control if it was added before. Otherwise, this will show a warning pathspec 'node_modules' did not match any files, which has no side effects and you can safely ignore. The flags cause the removal to be recursive and include the cache.

git status displays the new changes. A change to .gitignore will appear, while node_modules will not appear as it is no longer being tracked by git.

Respondent: Andrew Schreiber

Solution #3:

**/node_modules

** is used for a recursive call in the whole project

Two consecutive asterisks ** in patterns matched against full pathname may have special meaning:

A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the same as pattern foo. **/foo/bar matches file or directory bar anywhere that is directly under directory foo.

A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth.

A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/**/b matches a/b, a/x/b, a/x/y/b and so on.

Other consecutive asterisks are considered invalid.

Why this approach is better than node_modules/

The ** acts as a recursive pattern. It is useful in monorepo projects where you have node_modules in sub directories. ** will search for all the node_modules inside the directory & ignore them.

Reference

Respondent: Rajendran Nadar

Solution #4:

First and foremost thing is to add .gitignore file in my-app. Like so in image below.

Put .gitignore in the parent folder/directory of node_modules.

and next add this in your .gitignore file

/node_modules

Note

You can also add others files too to ignore them to be pushed on github. Here are some more files kept in .gitignore. You can add them according to your requirement. # is just a way to comment in .gitignore file.

# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
/node_modules

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
Respondent: Mustkeem K

Solution #5:

Adding below line in .gitignore will ignore node modules from the entire repository.

node_modules

enter image description here

Respondent: Umesh Khurana

Solution #6:

Create .gitignore file in root folder directly by code editor or by command

For Mac & Linux

 touch .gitignore 

For Windows

 echo >.gitignore 

open .gitignore declare folder or file name like this /foldername

Respondent: Amit Kumar

Solution #7:

**node_modules

This works for me

recursive approach to ignore all node_modules present in sub folders

Respondent: Hanzla Habib

Solution #8:

it will automatically create a .gitignore file if not then create a file name .gitignore
and add copy & paste the below code

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

these below are all unnecessary files

See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

and save the .gitignore file and you can upload

Respondent: Shaik Muneer

Solution #9:

you can do it with SVN/Tortoise git as well.

just right click on node_modules -> Tortoise git -> add to ignore list.

This will generate .gitIgnore for you and you won’t find node_modules folder in staging again.

Respondent: Dhruvesh Patel

Solution #10:

If your subproject/client node_modules gets committed,

    # dependencies
    /node_modules
    /.pnp
    .pnp.js

    # testing
    /coverage

    # production
    /build

    # misc
    .DS_Store
    .env.local
    .env.development.local
    .env.test.local
    .env.production.local

    npm-debug.log*
    yarn-debug.log*
    yarn-error.log*

then add “node_modules” at the last line.

    # dependencies
    /node_modules
    /.pnp
    .pnp.js

    # testing
    /coverage

    # production
    /build

    # misc
    .DS_Store
    .env.local
    .env.development.local
    .env.test.local
    .env.production.local

    npm-debug.log*
    yarn-debug.log*
    yarn-error.log*
    node_modules 
    # ------ Up Here ------
Respondent: Byram

Solution #11:

Follow these steps –

  • open git bash on the folder where your project is, or open vs code terminal
    by hitting
CTRL + `
  • write, [ echo > .gitignore ] in the terminal or, create a file [.gitignore] directly into the folder
  • then write this to ignore node modules from entire repository
node_modules
  • or, try this to ignore all node_modules from multiple sub folders
**node_modules

Note : if you make spelling mistake while typing the name of the folder or file, it won’t work. so, double check spelling

Respondent: JoyShaheb

Solution #12:

Add below line to your .gitignore

*/node_modules/*

This will ignore all node_modules in your current directory as well as subdirectory.

Respondent: Manish Dubey

Solution #13:

just add different .gitignore files to mini project 1 and mini project 2. Each of the .gitignore files should /node_modules and you’re good to go.

Respondent: Alpha Dzingina

Solution #14:

foe the ones who are trying the answers above and still facing the problem

I’ve tried almost all of the answers eventually , it fixed my problem with ignoring node_modules but only after i committed the changes !

Respondent: Harvester Haidar

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy