Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to remove node_modules after they have been added to a repo

How to remove node_modules

  1. Create a .gitignore file in the git repository if it does not contain one

touch .gitignore

  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

  1. Commit the git repository without the node modules folder

git commit -m "Removed node_module folder"

  1. Push the repository to github

git push origin master

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

git add .gitignore

git commit -m "Updated the .gitignore file

git push origin master

@JeffNewcomer

This comment has been minimized.

Copy link

@JeffNewcomer JeffNewcomer commented Sep 20, 2018

Thank you!

@shubhojitroy

This comment has been minimized.

Copy link

@shubhojitroy shubhojitroy commented Oct 7, 2018

thanks very much was helpful

@mckenzma

This comment has been minimized.

Copy link

@mckenzma mckenzma commented Oct 27, 2018

will this work if the node_modules folder is in a subfolder in the repo? if not, how can I adjust the above commands to do that?

@mareestephan

This comment has been minimized.

Copy link

@mareestephan mareestephan commented Nov 19, 2018

Thank you!

@adebigare

This comment has been minimized.

Copy link

@adebigare adebigare commented Feb 14, 2019

FINALLY! Thanks so much for this. All of the other threads in (SO etc) don't tell you to specifically remove the node_modules directory, and that was the only way to get it out of the repo. You're a lifesaver.

@campbellmarianna

This comment has been minimized.

Copy link

@campbellmarianna campbellmarianna commented Mar 9, 2019

Thank you! This is very helpful.

@maitrungduc1410

This comment has been minimized.

Copy link

@maitrungduc1410 maitrungduc1410 commented Mar 14, 2019

@mackenzma

will this work if the node_modules folder is in a subfolder in the repo? if not, how can I adjust the above commands to do that?

Yes it also works when node_modules in subfolder of the project. I did it

@tkhan236

This comment has been minimized.

Copy link

@tkhan236 tkhan236 commented Apr 4, 2019

Thanks, very helpful.

@joselvelez

This comment has been minimized.

Copy link

@joselvelez joselvelez commented May 22, 2019

Very helpful! Thanks!

@j8jacobs

This comment has been minimized.

Copy link

@j8jacobs j8jacobs commented Jun 12, 2019

haha this is great, used it for reference when i commited my react build folder after some project restructuring :) thanks!

@aamorozov

This comment has been minimized.

Copy link

@aamorozov aamorozov commented Jun 12, 2019

Spent some time trying to figure out why some of the node_modules are still being added to git. This solved it, thank you!

@Jeremy720

This comment has been minimized.

Copy link

@Jeremy720 Jeremy720 commented Jun 13, 2019

thank you!

@jeanmachuca

This comment has been minimized.

Copy link

@jeanmachuca jeanmachuca commented Jun 20, 2019

Hello, this is something really useful! I've applied it to https://github.com/QuickCorp/QCObjects. Thank you very much!

@hasanuzzamanbe

This comment has been minimized.

Copy link

@hasanuzzamanbe hasanuzzamanbe commented Jul 2, 2019

thanks, very helpfull

@arifcseewu

This comment has been minimized.

Copy link

@arifcseewu arifcseewu commented Jul 19, 2019

Thanks a lot. you save my day :)

@chinyun

This comment has been minimized.

Copy link

@chinyun chinyun commented Jul 20, 2019

Thank you! :)

@CheviSutt

This comment has been minimized.

Copy link

@CheviSutt CheviSutt commented Aug 28, 2019

Worked great, don't want to be that guy but the < git commit -m "Updated the .gitignore file > command above is missing the final double quote. Thanks for the post!

@AwesomeZaidi

This comment has been minimized.

Copy link

@AwesomeZaidi AwesomeZaidi commented Sep 27, 2019

Thank you! This is very helpful.

Haha found you in the wilderness Marianna!! 😄

@almeidacavalcante

This comment has been minimized.

Copy link

@almeidacavalcante almeidacavalcante commented Oct 25, 2019

Event though you removed the node_modules from the repository, this folder remains in the history of the repository. So, if your repo should have no more then 1 MB it will have 33MB because of the node_module folder.

How can I get rid of this node_modules from my repository history?

Thank you.

@buaiscia

This comment has been minimized.

Copy link

@buaiscia buaiscia commented Oct 30, 2019

thank you, really needed, didn't know about the caching trick!

@wdgrantham

This comment has been minimized.

Copy link

@wdgrantham wdgrantham commented Oct 30, 2019

Thanks for this! Should have been paying more attention before I committed all my node files, lol.

@Himank17Gupta

This comment has been minimized.

Copy link

@Himank17Gupta Himank17Gupta commented Nov 8, 2019

Thanks man...been struggling for quite a few hours..never imagined cached files can be troublesome as well

@mourajrs

This comment has been minimized.

Copy link

@mourajrs mourajrs commented Nov 12, 2019

Thank you!

@iamsarthakjoshi

This comment has been minimized.

Copy link

@iamsarthakjoshi iamsarthakjoshi commented Nov 17, 2019

Life saver. Thank you!

@HuakunShen

This comment has been minimized.

Copy link

@HuakunShen HuakunShen commented Nov 19, 2019

thank you

@ameerpro

This comment has been minimized.

Copy link

@ameerpro ameerpro commented Nov 28, 2019

This was really simple. Thanks

@alexlepore

This comment has been minimized.

Copy link

@alexlepore alexlepore commented Nov 30, 2019

Thanks!

@MichaelNeely77

This comment has been minimized.

Copy link

@MichaelNeely77 MichaelNeely77 commented Dec 26, 2019

Thank you!

@GitOffMyCode

This comment has been minimized.

Copy link

@GitOffMyCode GitOffMyCode commented Jan 25, 2020

super helpful - thanks!

@etze

This comment has been minimized.

Copy link

@etze etze commented Jan 28, 2020

thanks

@citizen-dror

This comment has been minimized.

Copy link

@citizen-dror citizen-dror commented Jan 30, 2020

Thank you!!

@franzamd

This comment has been minimized.

Copy link

@franzamd franzamd commented Mar 24, 2020

Thank you!! This helped me a lot when starting with git :)

@oguzzarci

This comment has been minimized.

Copy link

@oguzzarci oguzzarci commented Apr 1, 2020

Thank you !

@ZumDeWald

This comment has been minimized.

Copy link

@ZumDeWald ZumDeWald commented Apr 9, 2020

Exactly what was needed, thank you for sharing!

@verahsu860604

This comment has been minimized.

Copy link

@verahsu860604 verahsu860604 commented Apr 13, 2020

Huge thanks, it works!

@elinalsterbo

This comment has been minimized.

Copy link

@elinalsterbo elinalsterbo commented Apr 18, 2020

Thank you!

@edenv30

This comment has been minimized.

Copy link

@edenv30 edenv30 commented May 11, 2020

Thank you!!

@ahmadtech199

This comment has been minimized.

Copy link

@ahmadtech199 ahmadtech199 commented May 14, 2020

Thank you so much!!

@nikitagupta0809

This comment has been minimized.

Copy link

@nikitagupta0809 nikitagupta0809 commented May 26, 2020

Thanks a lot! Worked for me!

@jakescript

This comment has been minimized.

Copy link

@jakescript jakescript commented Jun 28, 2020

Appreciate you <3

@Dhreeves

This comment has been minimized.

Copy link

@Dhreeves Dhreeves commented Jul 1, 2020

Thankssss!

@ARTfun00

This comment has been minimized.

Copy link

@ARTfun00 ARTfun00 commented Jul 7, 2020

Thanks!

@cfayard

This comment has been minimized.

Copy link

@cfayard cfayard commented Jul 15, 2020

👍👍

@thananjeyanstyluma

This comment has been minimized.

Copy link

@thananjeyanstyluma thananjeyanstyluma commented Jul 17, 2020

Thank you!

@dyingSatyr

This comment has been minimized.

Copy link

@dyingSatyr dyingSatyr commented Jul 21, 2020

Thanks!

@eduuh

This comment has been minimized.

Copy link

@eduuh eduuh commented Jul 22, 2020

Thanks alot

@sanketghanmare

This comment has been minimized.

Copy link

@sanketghanmare sanketghanmare commented Jul 23, 2020

Thank you

@pemeraldy

This comment has been minimized.

Copy link

@pemeraldy pemeraldy commented Jul 27, 2020

Awesome, thanks.

@jainesenna

This comment has been minimized.

Copy link

@jainesenna jainesenna commented Aug 14, 2020

Thank you

@3sc0b3d0

This comment has been minimized.

Copy link

@3sc0b3d0 3sc0b3d0 commented Aug 21, 2020

Lifesaver, thanks!

@amerzur

This comment has been minimized.

Copy link

@amerzur amerzur commented Sep 6, 2020

Thanks. , work 100%

@kikaiaku

This comment has been minimized.

Copy link

@kikaiaku kikaiaku commented Sep 12, 2020

thank you so much

@mdiaz2307

This comment has been minimized.

Copy link

@mdiaz2307 mdiaz2307 commented Sep 23, 2020

Thank you very much

@Amol-Shrivastava

This comment has been minimized.

Copy link

@Amol-Shrivastava Amol-Shrivastava commented Oct 1, 2020

hey, @Imcneel thank you for this code. Can you suggest how can I remove the JSON file from my git repo? I added them to my .gitignore but still, they are present in my repo .

@uddiptagogoi2000

This comment has been minimized.

Copy link

@uddiptagogoi2000 uddiptagogoi2000 commented Oct 15, 2020

Thanks a lot

@Jollyuser7

This comment has been minimized.

Copy link

@Jollyuser7 Jollyuser7 commented Oct 22, 2020

Thank you

@Tabele86

This comment has been minimized.

Copy link

@Tabele86 Tabele86 commented Oct 27, 2020

MVP!!! I didn't want to make new repos since I had set these two on heroku already. You'd think the Git Desktop would let you delete easily but noooo.

@maxizhukov

This comment has been minimized.

Copy link

@maxizhukov maxizhukov commented Dec 2, 2020

Great, thanks)))

@allanleonardjr

This comment has been minimized.

Copy link

@allanleonardjr allanleonardjr commented Jan 6, 2021

Just what I needed, thanks!

@jdhprogrammer

This comment has been minimized.

Copy link

@jdhprogrammer jdhprogrammer commented Jan 18, 2021

Perfect. works like a charm.

@wjutterback

This comment has been minimized.

Copy link

@wjutterback wjutterback commented Jan 25, 2021

Awesome! Thank you very much

@CodingKrills

This comment has been minimized.

Copy link

@CodingKrills CodingKrills commented Feb 5, 2021

worked very well thankyou !

@Trend20

This comment has been minimized.

Copy link

@Trend20 Trend20 commented Feb 11, 2021

Thanks for this solution. It worked for me

@wladimirnavega

This comment has been minimized.

Copy link

@wladimirnavega wladimirnavega commented Feb 12, 2021

Thank you very much!!!

@christianbmartinez

This comment has been minimized.

Copy link

@christianbmartinez christianbmartinez commented Feb 17, 2021

You helped a lot of people with this including me! Great job! I am getting very comfortable with git now, thanks to various write ups like these.

@sudhirdontha72

This comment has been minimized.

Copy link

@sudhirdontha72 sudhirdontha72 commented Mar 7, 2021

easy-peasy! Thanks a lot for your gist!

@shohey1226

This comment has been minimized.

Copy link

@shohey1226 shohey1226 commented Mar 15, 2021

🙏 Thanks!

@MarcioBBS

This comment has been minimized.

Copy link

@MarcioBBS MarcioBBS commented Mar 25, 2021

I much appreciate it!

@neverkas

This comment has been minimized.

Copy link

@neverkas neverkas commented Mar 31, 2021

Thanks..!

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