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 2. Open up the .gitignore and add the following line to the file

node_modules 3. Remove the node_modules folder from the git repository

git rm -r --cached node_modules 4. Commit the git repository without the node modules folder

git commit -m "Removed node_module folder" 5. 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 commented Sep 20, 2018

Thank you!

@shubhojitroy

This comment has been minimized.

Copy link

shubhojitroy commented Oct 7, 2018

thanks very much was helpful

@mckenzma

This comment has been minimized.

Copy link

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 commented Nov 19, 2018

Thank you!

@adebigare

This comment has been minimized.

Copy link

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 commented Mar 9, 2019

Thank you! This is very helpful.

@maitrungduc1410

This comment has been minimized.

Copy link

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 commented Apr 4, 2019

Thanks, very helpful.

@joselvelez

This comment has been minimized.

Copy link

joselvelez commented May 22, 2019

Very helpful! Thanks!

@j8jacobs

This comment has been minimized.

Copy link

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 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 commented Jun 13, 2019

thank you!

@jeanmachuca

This comment has been minimized.

Copy link

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 commented Jul 2, 2019

thanks, very helpfull

@arifcseewu

This comment has been minimized.

Copy link

arifcseewu commented Jul 19, 2019

Thanks a lot. you save my day :)

@chinyun

This comment has been minimized.

Copy link

chinyun commented Jul 20, 2019

Thank you! :)

@CheviSutt

This comment has been minimized.

Copy link

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 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 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 commented Oct 30, 2019

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

@wdgrantham

This comment has been minimized.

Copy link

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 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 commented Nov 12, 2019

Thank you!

@iamsarthakjoshi

This comment has been minimized.

Copy link

iamsarthakjoshi commented Nov 17, 2019

Life saver. Thank you!

@HuakunShen

This comment has been minimized.

Copy link

HuakunShen commented Nov 19, 2019

thank you

@ameerpro

This comment has been minimized.

Copy link

ameerpro commented Nov 28, 2019

This was really simple. Thanks

@alexlepore

This comment has been minimized.

Copy link

alexlepore commented Nov 30, 2019

Thanks!

@MichaelNeely77

This comment has been minimized.

Copy link

MichaelNeely77 commented Dec 26, 2019

Thank you!

@GitOffMyCode

This comment has been minimized.

Copy link

GitOffMyCode commented Jan 25, 2020

super helpful - thanks!

@etze

This comment has been minimized.

Copy link

etze commented Jan 28, 2020

thanks

@citizen-dror

This comment has been minimized.

Copy link

citizen-dror commented Jan 30, 2020

Thank you!!

@franzamd

This comment has been minimized.

Copy link

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 commented Apr 1, 2020

Thank you !

@ZumDeWald

This comment has been minimized.

Copy link

ZumDeWald commented Apr 9, 2020

Exactly what was needed, thank you for sharing!

@verahsu860604

This comment has been minimized.

Copy link

verahsu860604 commented Apr 13, 2020

Huge thanks, it works!

@elinalsterbo

This comment has been minimized.

Copy link

elinalsterbo commented Apr 18, 2020

Thank you!

@edenv30

This comment has been minimized.

Copy link

edenv30 commented May 11, 2020

Thank you!!

@ahmadtech199

This comment has been minimized.

Copy link

ahmadtech199 commented May 14, 2020

Thank you so much!!

@nikitagupta0809

This comment has been minimized.

Copy link

nikitagupta0809 commented May 26, 2020

Thanks a lot! Worked for me!

@jakescript

This comment has been minimized.

Copy link

jakescript commented Jun 28, 2020

Appreciate you <3

@Dhreeves

This comment has been minimized.

Copy link

Dhreeves commented Jul 1, 2020

Thankssss!

@ARTfun00

This comment has been minimized.

Copy link

ARTfun00 commented Jul 7, 2020

Thanks!

@cfayard

This comment has been minimized.

Copy link

cfayard commented Jul 15, 2020

👍👍

@thananjeyanstyluma

This comment has been minimized.

Copy link

thananjeyanstyluma commented Jul 17, 2020

Thank you!

@dyingSatyr

This comment has been minimized.

Copy link

dyingSatyr commented Jul 21, 2020

Thanks!

@eduuh

This comment has been minimized.

Copy link

eduuh commented Jul 22, 2020

Thanks alot

@sanketghanmare

This comment has been minimized.

Copy link

sanketghanmare commented Jul 23, 2020

Thank you

@pemeraldy

This comment has been minimized.

Copy link

pemeraldy commented Jul 27, 2020

Awesome, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.