Skip to content

Instantly share code, notes, and snippets.

@lmcneel
Last active April 12, 2024 00:28
Show Gist options
  • Save lmcneel/45594e550a3403d589bdcaad38138a83 to your computer and use it in GitHub Desktop.
Save lmcneel/45594e550a3403d589bdcaad38138a83 to your computer and use it in GitHub Desktop.
How to remove node_modules after they have been added to a repo

How to remove node_modules

Create a .gitignore file

  1. Check for an existing .gitignore file in the project directory
ls -a
  1. If your project directory has a .gitingore file - skip to step 3. Otherwise, continue with:

Create a .gitignore file in the git repository

touch .gitignore

Remove the node_modules directory

  1. Open up the .gitignore and add the following line to the file
**/node_modules
  1. Remove the node_modules folder from the git repository
git rm -r --cached node_modules

Commit All Changes

  1. Commit the git repository without the node_modules folder
git commit -m "Removed node_modules folder"
  1. Push the change to the remote repo
git push origin main
  1. Commit the .gitignore file
git add .gitignore
git commit -m "Updated the .gitignore file"
git push origin main
@MarcioBBS
Copy link

I much appreciate it!

@neverkas
Copy link

Thanks..!

@Godwin-NJ
Copy link

Nice one, was helpful

@PatriciaFeio
Copy link

Really helpful!

@vartikavr
Copy link

Really helpful. Thanks!

@nosremetnarg
Copy link

Here to say thanks!

@denokenya
Copy link

Thanks

@RadhikaRJ
Copy link

Thank you for this solution. It works & was helpful.

@enti-re
Copy link

enti-re commented May 30, 2021

Finally removed
Thanks for the article

@sajithatharaka
Copy link

Thanks a lot !

@Zeng95
Copy link

Zeng95 commented Jun 12, 2021

Thank you very much! It works like a charm.

@willianfrancas
Copy link

Thank you so much!!

@holylander
Copy link

thanks mate

@yeica
Copy link

yeica commented Aug 14, 2021

thank you, this was so helpful ❤

@jfgrissom
Copy link

Looks like this gift is still giving. Thanks!

@gjqeriqi
Copy link

Thanks, dude !!

@ismailakinkunmi
Copy link

Thank you.

@DanTrausch
Copy link

The is a missing a quote after the message for committing the .gitignore file. Since I cant fork and create a pull request for a gist, below are the recommended changes I made in a copy.

6) After all of that, you should also add the gitignore and commit it to the repository

```bash
git add .gitignore
git commit -m "Updated the .gitignore file"
git push origin master
```

@lmcneel
Copy link
Author

lmcneel commented Dec 15, 2021

@SeaRauber Thanks for the catch on the missing quotes and the suggestion. I updated with your suggested change - and made an attempt to update the format overall. However, I'm not happy with that formatting. I'll make another commit that cleans it up more later today.

@denokenya
Copy link

Thanks alot

@ruhulamin77
Copy link

Thank you so much!

@dornescum
Copy link

Thank you

@wgbcamp
Copy link

wgbcamp commented Jan 30, 2022

You're a wizard!

@alexisnapoles
Copy link

Thank you so much! Saved me so much time.

@freakfan15
Copy link

Thanks a lot.

@maidul-iut
Copy link

Superb. Worked for me in 1st time. Thanks a lot.

@sparker888
Copy link

whew. Thx!

@ZeligHerskovits
Copy link

Wow thanks!!

@keberlea
Copy link

Thanks for the help!

@Baptcave
Copy link

Thank you so much !

@BekzodIsakov
Copy link

Don't we have to edit **/node_modules back to node_modules inside .gitignore file?

@lmcneel
Copy link
Author

lmcneel commented May 30, 2023

Don't we have to edit **/node_modules back to node_modules inside .gitignore file?

@BekzodIsakov A leading **/ in a .gitignore file will allow files and folders to be ignored in the current or any descendant directory rather than just the current directory. This is useful in apps with separate packages for the client & server, for example.

If I have a directory structure like

  • .gitignore
  • client
    • node_modules
  • server
    • node_modules

The **/node_modules will still ignore the node_modules folder.

The **/ is not necessary if only the node_modules folder will be at the same level as the .gitignore file.

@BekzodIsakov
Copy link

I got it. Thanks!

@gabrielfogaca
Copy link

Thanks!

@MPDADDY
Copy link

MPDADDY commented Aug 1, 2023

Thanks, that was helpful.

@JustCLE4R
Copy link

thanks thats very helpful, my brain ass just push everything without looking what i push until i push node_modules LOL

@thelittlemitak
Copy link

thanks thats very helpful, my brain ass just push everything without looking what i push until i push node_modules LOL
jijij I did the same. thanks a lot!

@DavidJRRJ
Copy link

Thanks!

@polooner
Copy link

omg timeless post already

@Salamandrachka
Copy link

you are the best! saved me a lot of time!!! thanks)))

@jendahorak
Copy link

thanks, tried chatGPT,, but fucker got me sidetracked, this helped

@chimaris
Copy link

Thanks for this.

@aj910
Copy link

aj910 commented Sep 28, 2023

Thanks for this, very helpful!

@InjuSmol
Copy link

InjuSmol commented Nov 9, 2023

❤️

@rendi12345678
Copy link

thanks man, this is work in my machine :)

@saadjangdaa
Copy link

Thanks its working

@benjackson33
Copy link

Thanks for this, very helpful

@alperbayram
Copy link

thanks 💯

@joshuajcarter
Copy link

Just perfect! Thank you.

@ApurvaBasule
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment