Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
  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
Copy link


Copy link

Nice one, was helpful

Copy link

Really helpful!

Copy link

Really helpful. Thanks!

Copy link

Here to say thanks!

Copy link


Copy link

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

Copy link

enti-re commented May 30, 2021

Finally removed
Thanks for the article

Copy link

Thanks a lot !

Copy link

Zeng95 commented Jun 12, 2021

Thank you very much! It works like a charm.

Copy link

Thank you so much!!

Copy link

thanks mate

Copy link

yeica commented Aug 14, 2021

thank you, this was so helpful

Copy link

Looks like this gift is still giving. Thanks!

Copy link

Thanks, dude !!

Copy link

Thank you.

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

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

Copy link

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.

Copy link

Thanks alot

Copy link

Thank you so much!

Copy link

Thank you

Copy link

wgbcamp commented Jan 30, 2022

You're a wizard!

Copy link

Thank you so much! Saved me so much time.

Copy link

Thanks a lot.

Copy link

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

Copy link

whew. Thx!

Copy link

Wow thanks!!

Copy link

Thanks for the help!

Copy link

Thank you so much !

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