11

I am trying to push my project to GitHub and Heroku. Since I am using Node.js in my app, I have folder node_modules. Before I commit I have created a file called .gitignore to ignore the Node.js modules.

Inside the file I have added folder node_modules and tried to push to the GitHub repository from the comandline/Terminal. I was not expecting the Node.js modules to be pushed, but they appeared finally in the repository. Is there an ways I have to follow?

Here is how I resolve my issue

 - $ git rm -r --cached node_modules

Add folder node_modules/ to your .gitignore file:

 - $ git commit -m "remove the ignored directory node_modules"

And then

 - $ git push origin master

After that, if you refresh the GitHub page you will see the node_modules folder gone.

3
  • 1
    Please provide your .gitignore file Commented May 24, 2017 at 21:21
  • 2
    Files that git already track won't be ignored by .gitignore, that file only provides rules for git add. If you've already added them, you need different tools to get rid of them. Commented May 24, 2017 at 21:25
  • .gitignore was already have node_modules to be ignored but for some reason when I pushed my code the node_modules pushed together with the other code. Commented Aug 18, 2017 at 4:26

4 Answers 4

29

The node_modules folder should be added to the .gitignore file.

First, get rid of your node_modules:

$ git rm -r --cached node_modules  
$ git commit -m "remove node_modules"

Then, push your change

$ git push origin master

Refresh the page to see the desired effect.

Sign up to request clarification or add additional context in comments.

Comments

4

First, you need to add node_modules to your .gitignore file.

Then

$ git rm -r -chached node_modules
$ git commit -m "remove node_modules"

And then:

$ git push origin master

2 Comments

Re "add node_modules to your .gitignore file.": How exactly? Somebody else may want to chime in.
Add the line "node_modules" (no quotes) in your .gitignore file
3

You have to tell Git to untrack the files that have been already added to Git (before you ignored them):

git rm --cached FILE

FILE won't be deleted from your filesystem. Git will untrack this fille, and since it's ignored, it won't be tracked anymore.

Then, of course you have to commit and push this change.

Comments

2

Just in case, is the .gitignore file inside the node_modules or in the root of the project?

If so, I'll recommend to place it (at least) in a parent folder, or ideally in the project's root, and inside it call node_modules as a relative path to the location of the file. i.e: some-path/node_modules.

Besides that, once the folder is committed and pushed, you should ignore the .gitignore file (by deleting it temporarily or deleting the node_modules line), so Git can keep track of the files, delete the folder, commit, push, and restore the .gitignore file.

1 Comment

What does "If so" refer to? The former or the latter? Can you make it more clear? Please respond by editing your question, not here in comments (without "Edit:", "Update:", or similar).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.