684

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/*

18 Answers 18

1326

Add node_modules/ or node_modules to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders like the below image.

example

6
  • what would be the glob path to do the same for karma, excludes? Commented Apr 7, 2016 at 13:11
  • 18
    For a gitignore template for nodejs: github.com/github/gitignore/blob/master/Node.gitignore Commented Mar 2, 2017 at 9:46
  • 27
    See @AndrewSchreiber's answer if you have previously added the node_modules folder to git. You'll need to remove it from the cache with "git rm -r --cached node_modules".
    – Casper
    Commented Feb 1, 2019 at 11:07
  • After enabling this, 10000+ file commits saved which was just not letting me do anything with git :D
    – m4n0
    Commented Jul 10, 2021 at 10:51
  • You don't need the slash at the end Commented Aug 12, 2021 at 18:09
314

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.

7
  • 6
    touch shouldn't be necessary here: >> will create the file if it doesn't exist.
    – lionello
    Commented May 7, 2018 at 7:55
  • 2
    touch is needed. >> will return no such file or directory: .gitignore otherwise. Commented May 9, 2018 at 22:31
  • 2
    Hmm, seems to differ from OS to OS
    – lionello
    Commented May 10, 2018 at 9:41
  • 2
    echo "node_modules/" > .gitignore should create a new file with original content, using >> appends text onto the end of existing specified file
    – chrisz
    Commented Sep 24, 2019 at 6:26
  • you need to remove th " " around the node_modules in windows cmd Commented Nov 16, 2020 at 12:55
129

Edit - (After 2022-04-09)

In a new monorepo setup I found just using this

node_modules

solved it to ignore all the node_modules in the subdirectory, note there is no slash before or after which means recursive.

Reference

Old Way - (Before 2022-04-09)

**/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

5
  • 4
    Care to explain why this approach is better than simply node_modules/?
    – Advena
    Commented May 7, 2020 at 9:55
  • 5
    In case of a monorepo, you could have several frontends to deal with. This approach will recursively ignore all the node_modules folders, independently of where they are located (e.g. /BackOffice/frontend/node_modules) Commented Nov 26, 2020 at 9:48
  • 3
    You don't need the **, it works the same without it Commented Aug 12, 2021 at 18:09
  • 1
    That didn't work for me, that is the reason I created this answer. For react native monorepo & firebase with cloud functions. Commented Aug 12, 2021 at 18:12
  • This is wild. 'wanna go for a walk?'' Blow them out of existence. That'l fix it.Put a disclaimer on your 'code'.
    – Pogrindis
    Commented Sep 14, 2022 at 4:32
37

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*
5
  • 2
    Why would you add build to the .gitignore? Commented Aug 13, 2018 at 15:19
  • 12
    @SebastianNielsen The same reason we don't track node_modules. You get build/ by compiling.
    – Dan
    Commented Oct 25, 2018 at 12:51
  • 1
    Do not add the slash at the start, it stops this from being recursive Commented Aug 12, 2021 at 18:10
  • @SoftwareEngineer can you create UR
    – Mustkeem K
    Commented Aug 13, 2021 at 18:48
  • @MustkeemK - sorry, what is UR? Commented Aug 13, 2021 at 22:10
14

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

node_modules

enter image description here

1
7

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

1
  • 1
    I guess you meant echo >.gitignore. ;)
    – lapo
    Commented Oct 5, 2020 at 14:15
6
**node_modules

This works for me

recursive approach to ignore all node_modules present in sub folders

5

If you wanna do it via the command line, type in echo node_modules/ > .gitignore. You can check if git is tracking the folder by typing in git status. If its being tracked type in git rm -r --cached node_modules.

4

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

3

Add below line to your .gitignore

/node_modules/

In my case, writing /node_modules without the foreslash after was not working

1

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 ------
1

If you are using the Sourcetree app in Mac or Windows

  1. Open sourcetree
  2. click particular project
  3. click settings
  4. click Advanced
  5. click Edit gitignore
  6. Write "node_modules"
  7. And Save.

For more information about this free Git client visit https://www.sourcetreeapp.com/

0

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.

0

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

0

Add node_modules/ or node_modules to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders.

0

For Windows 8/10/11/12

- echo "node_modules/" >> .gitignore (create gitignore file)
- git rm -r --cached node_modules (remove node module if add)
- git status
-2

Add below line to your .gitignore

*/node_modules/*

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

1
  • Note that there is already an accepted answer to this question. Please edit your answer to ensure that it improves upon other answers already present in this question.
    – hongsy
    Commented Jan 30, 2020 at 7:28
-3

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.

2
  • 2
    I think this is what the original poster wanted to avoid. The question asked how to make a single .gitignore change to exclude all node_modules folders. Commented Jul 5, 2020 at 20:46
  • 1
    oh...he wasn't specific about having just 1 .gitignore file Commented Jul 7, 2020 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.