Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable `.gitignore` For Node.js

Tree structure

That's how my Node.js project is organized:

/
| - node_modules               [+ INCLUDE]
|   | - my-mod1
|   |   | - node_modules       [- IGNORE]
|   |   |   | - external-mod1 
|   |   |   | - external-mod2 
|   |   | - src
|   |   |   | - node_modules   [+ INCLUDE]
|   |   |   |   | - my-mod2 
|   |   |   |   | - my-mod3
|   | - my-mod4

My plan

When publishing my project to GitHub:

  • I want to include my-mods.
  • I don't want to include the external-mods.

That means:

  • I want to include the top-level /node_modules folder.
  • I don't want to include node_modules folders which are direct childs of a module folder.
  • But I want to include node_moduels folders which are childs of a src folder.

What I did

I added the following lines to /.gitignore:

#################  
## npm
#################

npm-debug.log
node_modules/
!/node_modules/
!src/node_modules/

My question

Which .gitignore rules do I need to include the right node_modules folders (as described above)?

Thanks - if anything's unclear, please comment.

like image 515
fdj815 Avatar asked Feb 19 '13 16:02

fdj815


People also ask

What should I Gitignore in node JS project?

gitignore is a simple text file containing a list of files and directories you wish to exclude from the git repository. Any file or directory that matches a specific pattern specified in the . gitignore file is ignored from your project. In our case, we need to exclude the node_modules directory from our repository.

What is Gitignore in node JS?

Git is a source control system that is used for tracking and merging changes in code. If there's files we want to push into source code for some reason we can use the . gitignore syntax.

What should I put for Gitignore?

The types of files you should consider adding to a . gitignore file are any files that do not need to get committed. You may not want to commit them for security reasons or because they are local to you and therefore unnecessary for other developers working on the same project as you.

How do I create a Gitignore node module?

gitignore file, simply create an empty text file in the root of your git repository and add the single line containing node_modules . You can find the reference and examples for . gitignore at Git - gitignore Documentation (git-scm.com).


2 Answers

node_modules/**/node_modules should work for what you are trying to do.

Tip: GitHub provides standard .gitignore's for various languages like Node.

like image 23
ben.snape Avatar answered Oct 05 '22 12:10

ben.snape


Since your structure is quite organized you could use this pattern to accomplish the task.

/node_modules/*/node-modules/ 

The above pattern will ignore node_modules under module folders, my-mod1,my-mod4 and others.

The above line will still allow the node_modules under src directory to be included when you push to github.

like image 172
functionoid Avatar answered Oct 05 '22 12:10

functionoid