How to add a folder to the .gitignore - node.js

Maybe my Googling skills are lacking but this seems like a question that should give me back thousands of hits and yet, I can't find a straight answer.
Simple as this:
I do constant pushes to github to share with a remote developer. We both have npm and bower installed. No need to push these huge folders to github all the time. My node_modules are ignored. But I can't seem to get gitignore to ignore my bower_components folder
I'm not too savvy on cmd, I barely scratch the surface so if you ar going to suggest that, please don't assume I know what you are talking about. Otherwise if it is as easy as adding it to the file itself using an IDE, well, I did that and no cigar. Here is my .gtignore file for your review
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing- task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
bower_components
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
Am I missing anything? How do I make the bower_components ignored?
Thank you

If you want to ignore folder, you need a trailing slash:
bower_components/
Then check if the rule apply with git check-ignore -v (the -v is important if you want to see the exact .gitignore file and line which causes a file to be ignored)
git check-ignore -v -- bower_components/afile
If that does not apply, then remove the content of that folder from the history of that repo:
git rm --cached -r -- bower_components/
git add .
git commit -m "Remove bower_components folder"
Then the .gitignore rule will take effect immediatly.

Related

How can I pull request if i have over 14k changes, is it normal that node_modules is so big? [duplicate]

I have a project containing multiple other projects :
Main project
Mini project 1
Mini project 2
All containing node_modules folder. I want git to ignore the folder no matter where it is starting from the root folder. Something like this to add in .gitignore :
*node_modules/*
Add node_modules/
or node_modules
to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders like the below image.
Use the universal one-liner in terminal in the project directory:
touch .gitignore && echo "node_modules/" >> .gitignore && git rm -r --cached node_modules ; git status
It works no matter if you've created a .gitignore or not, no matter if you've added node_modules to git tracking or not.
Then commit and push the .gitignore changes.
Explanation
touch will generate the .gitignore file if it doesn't already exist.
echo and >> will append node_modules/ at the end of .gitignore, causing the node_modules folder and all subfolders to be ignored.
git rm -r --cached removes the node_modules folder from git control if it was added before. Otherwise, this will show a warning pathspec 'node_modules' did not match any files, which has no side effects and you can safely ignore. The flags cause the removal to be recursive and include the cache.
git status displays the new changes. A change to .gitignore will appear, while node_modules will not appear as it is no longer being tracked by git.
Edit - (Before 2022-04-09)
In a new monorepo setup I found just using this
node_modules
solved it to ignore all the node_modules in the subdirectory, note there is no slash before or after which means recursive.
Reference
Old Way - (Before 2022-04-09)
**/node_modules
** is used for a recursive call in the whole project
Two consecutive asterisks ** in patterns matched against full pathname may have special meaning:
A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the same as pattern foo. **/foo/bar matches file or directory bar anywhere that is directly under directory foo.
A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth.
A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/\**/b matches a/b, a/x/b, a/x/y/b and so on.
Other consecutive asterisks are considered invalid.
Why this approach is better than node_modules/
The ** acts as a recursive pattern. It is useful in monorepo projects where you have node_modules in sub directories. ** will search for all the node_modules inside the directory & ignore them.
Reference
First and foremost thing is to add .gitignore file in my-app. Like so in image below.
and next add this in your .gitignore file
/node_modules
Note
You can also add others files too to ignore them to be pushed on github. Here are some more files kept in .gitignore. You can add them according to your requirement. # is just a way to comment in .gitignore file.
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
Adding below line in .gitignore will ignore node modules from the entire repository.
node_modules
Create .gitignore file in root folder directly by code editor or by command
For Mac & Linux
touch .gitignore
For Windows
echo >.gitignore
open .gitignore declare folder or file name like this /foldername
**node_modules
This works for me
recursive approach to ignore all node_modules present in sub folders
it will automatically create a .gitignore file if not then create a file name .gitignore
and add copy & paste the below code
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
these below are all unnecessary files
See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
and save the .gitignore file and you can upload
Add below line to your .gitignore
/node_modules/
In my case, writing /node_modules without the foreslash after was not working
you can do it with SVN/Tortoise git as well.
just right click on node_modules -> Tortoise git -> add to ignore list.
This will generate .gitIgnore for you and you won't find node_modules folder in staging again.
If your subproject/client node_modules gets committed,
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
then add "node_modules" at the last line.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules
# ------ Up Here ------
Follow these steps -
open git bash on the folder where your project is, or open vs code terminal
by hitting
CTRL + `
write, [ echo > .gitignore ] in the terminal or, create a file [.gitignore] directly into the folder
then write this to ignore node modules from entire repository
node_modules
or, try this to ignore all node_modules from multiple sub folders
**node_modules
Note : if you make spelling mistake while typing the name of the folder or file, it won't work. so, double check spelling
Add node_modules/ or node_modules to the .gitignore file to ignore all directories called node_modules in the current folder and any subfolders.
In Mac,
Open sourcetree
click particular project
click settings
click Advanced
click Edit gitignore
Write "node_modules"
And Save.
If you wanna do it via the command line, type in echo node_modules/ > .gitignore. You can check if git is tracking the folder by typing in git status. If its being tracked type in git rm -r --cached node_modules.
Add below line to your .gitignore
*/node_modules/*
This will ignore all node_modules in your current directory as well as subdirectory.
just add different .gitignore files to mini project 1 and mini project 2. Each of the .gitignore files should /node_modules and you're good to go.
foe the ones who are trying the answers above and still facing the problem
I've tried almost all of the answers eventually , it fixed my problem with ignoring node_modules but only after i committed the changes !

git status is not showing my changed files

I have a Node.js project and have imported a few Node modules. The git_status command shows the files that I changed in the project. I have also changed some files under the node_modules directory but those are not shown. The node_modules directory is shown as untracked.
Untracked files:
(use "git add <file>..." to include in what will be committed)
node_modules/
src/js/main-release-paths.json
I have changed only one file in node_modules:
node_modules/#oracle/grunt-oraclejet/node_modules/#oracle/oraclejet-tooling/lib/serve/connect.js
How can I track this file?
You shouldn't add the node_modules folder to git then it's managed bei the npm command. So remove that folder and use the npm install command.
When you have added something in that folder it's shown as changed, if you had first added some files. So i hop that folder is in your .gitignore file and ignored. You should check that.
Assuming you have git repo, Please check the .gitignore files inside your project. Entries in .gitignore will be ignored by Git and will not show up when you do git add.
Adding the file to track
This is most likely not a suitable approach (read below). However , if you really want to do it:
In order to track a file nested under node_modules, you should add the directory. All of its content will be staged in git
git add node_modules
Recommended approach
You probably don't want to track the contents of node_modules directory, because:
running npm install will wipe your changes
hundreds or thousand files are likely to end up present in that directory
tracking package.json and package-lock.json in git is sufficient, then populate node_modules using npm install command.
You are not supposed to modify files located in node_modules directory directly. Instead, fork the module in question, modify it, and:
either publish your own version in npm
or reference it as a git repository inside package.json (how-to)
Then, also add node_modules directory to the .gitignore file.
If the whole directory is untracked, git status will show only the directory. Because it's untracked, there is no meaningful difference to git between changed and unchanged files in there.
Other answers have already addressed that tracking node_modules is nota good idea. Of you want to track your changes to modules, consider cloning the module repo and including it as a git submodule.
It should be inside .gitignore file. A .gitignore file should be committed into your repository, in order to share the ignore rules with any other users that clone the repository.

How to use .gitignore

I want to exclude node_modules directory except for one file.
This is what the relevant part of my .gitignore looks like - I have tried a few things but no luck:
.gitignore
....
# node.js
#
node_modules/
npm-debug.log
yarn-error.log
....
I think I need to do something like:
node_modules/
!node_modules/path/to/index.js
But it says here that the above won't work due to some git restriction. Anyway, the answer is probably in that link, I'm just having a little trouble figuring it out.
Your way:
node_modules/
!node_modules/path/to/index.js
Nope! Due to a performance-related quirk in Git, you can not negate a file that is ignored due to a pattern matching a directory.
I don't sure way to
git add .
to add your index.js and all file exclude node_modules
But you can use git add node_modules/path/to/index.js -f
-f because your file are ignored by one of your .gitignore files
and use node_modules/ in .gitignore
git won't ignore your index.js file (added it to cache).
Another solution is to move your react-native-calendars directory outside your node_modules directory, e.g move it to a directory name nmod2. and then when you import it, add the path like var react-cal = require('./nmod2/react-native-calendars'); instead of var react-cal = require('react-native-calendars');. So that you can put the whole node_modules directory in .gitignore. Hope it helps

Should I add yarn-error.log to my .gitignore file?

Yarn has created yarn.lock and yarn-error.log.
I have been told not to add yarn.lock to my .gitignore file because it locks down the packages.
Should I ignore the latter?
It makes sense to ignore the yarn-error.log—log files are only useful to debug your own copy of the code, so there's no need to upload it to the repository.
File should be uploaded to your repo when they are useful or needed to build your project. The yarn-error.log (as the name suggests) is an error log, so it's never read by Yarn. The whole point of it is that you read the log to find out what went wrong, and if you've not had any errors, it might not even exist at all.
gitignore.io, a service which generates .gitignore files, include yarn-error.log and yarn-debug.log in their .gitignore file for Node:
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
It may be wise to follow their example if you're not sure what you need—most pre-made .gitignore files have thought this issue through and concluded that logs should generally be ignored.
Since writing this I realise all log files are typically ignored in the .gitignore file with this entry:
*.log
Its system specific log file for the user. So it should be added to gitignore.
some times we find error that is "cannot find symbol" when build the project.
to solve that problem we add this file to our root project.
we can use it in our project where all all files and folder are available.
just above to yarn.log file

keep node_modules outside source tree in development (not production)

I prefer to keep all generated files and dependencies outside my source tree while I work on it.
npm and grunt make that difficult: npm will never allow moving local node_modules, so I have to use --global and --prefix. But grunt does not support such a scheme, apparently.
How can I achieve my objective given the above constraints?
So, if I have a project:
foo/
.git/
src/
gruntfile.js
package.json
I want no extra files in my tree, specifically, node_modules. (Also bower_components and build etc but this is about npm.) This directory should remain untouched while I am working on it and running it. That is all.
Apparently npm link is supposed to do this, but when I tried it still installed all the dependencies in ./node_modules. Any other invocation I cannot fathom; the docs are not clear.
A related suggestion was to use another directory with symlink to my gruntfile or package.json, but grunt just resolved the symlink and continued to work in my original directory!
So far the closest I have come is to link to e.g. ~/.cache/foo/node_modules from my project. Although it achieves keeping the deps out of my tree, I still have this link cluttering my workspace.
I want to know if there is a better way. Will some combination of npm install, npm link, ln, ~/.cache, NODE_PATH and PWD allow me to run my project, from my source tree, and keep it clean of all non-source artefacts?
Swimming against standards is a Very Bad Idea ®.
What you can (and should) do is add node_modules/ to your .gitignore (or whatever ignore file you have for your given source control system) so you don't version these files.
Also, you can use a directory like src/ to organize your code and "shelter" it from the mandatory configuration files (package.json, Gruntfile.coffee, etc).

Resources