How to ignore node_modules using .gitignore? - node.js

I'm not sure how to ignore files on .gitignore, I think it has 3 options but I don't know the correct one
/node_modules
.node_modules
.node_modules/
Please, can you tell me which is the correct one or if none of the 3 works, can you tell me how to ignore files on .gitignore?
I really appreciate any help you can provide.

use node_modules or /node_modules both should work also try to look at the node.gitignore, it will give you an idea on what to ignore in your project

the node_modules directory isn't hidden. You should just be able to do node_modules

Related

gitignore all node_modules directories and subdirectories

If I put this in .gitignore:
node_modules
it doesn't appear to ignore subdirectories that contain a node_modules folder.
So my guess is that this will work:
*/node_modules/
does anyone know what the right syntax is for ignore files to ignore matching subfolders?
You can ignore sub-directories indeed, and the syntax you proposed is correct, here is a working example of a .gitignore i'm using :
# Bundler
Gemfile.lock
bin/*
.bundle/*
*/test/

Is there anyway to safely clean up local Firebase project folder?

I'm new to Firebase. Recently, in order to fix certain errors, I tried many different ways until I feel that my local project folder is pretty much messed up, which could cause more other hidden problems. For example, I keep running npm init on many different folders, sometimes manually edited the package.json file, run npm install firebase#5.9.4 --save on several different folders and so on.
So I wonder, is there a way (or tool) that we can scan through our project folder and safely clean it up, e.g. remove redundant/unused dependencies, e.g. node_modules, add required dependencies, fix declarations/referencing errors, and so on without removing user-created assets like .js, .css, images etc.? I want to do that is because I believe this could potentially reduce many hidden problems and help to narrow down my troubleshooting tasks...
Alternatively, can I do the following:
Backup all my JS files.
Empty my Firebase's functions folder.
run firebase init again.
Restore my JS files.
Deploy and troubleshoot my Firebase functions again? Or any steps missing?
Sorry for a silly question. Any advice would be much appreciated, thank you!
If you think there is a problem with the contents of your node_modules folder:
Remove all node_modules folders completely. They don't contain any special information. It is just copies of modules downloaded from the internet.
Rebuild it completely with npm install from the correct location (your functions folder), which uses your package.json to determine what it should contain.

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

browserify-shim error when node_modules folder is a symlink

On the production server node_modules folder is a symbolic link for continuous deployment purposes.
When I run gulp command, I got many errors like this:
Error: Unable to find a browserify-shim config section in the package.json for /home/web/www/persist/node_modules/jquery-ui/jquery-ui.js while parsing file: /home/web/www/persist/node_modules/jquery-ui/jquery-ui.js]
filename: '/home/web/www/persist/node_modules/jquery-ui/jquery-ui.js'
. . .
If I move node_modules in project folder, build process is successfull. How to solve this problem?
Answer from thlorenz (author of browserify-shim)
Linking breaks browserify and shim since your projects dependencies
are outside of your project tree. So when looking upwards these tools
can't find the package.json of your package anymore.
So don't link your node_modules folders .. it's a bad idea anyways
since you're then linked to a global in your system, i.e. it's better
to have all your deps be local to your project. Not sure what your
deployment purposes are, but to me it seems like whoever made that
decision didn't fully understand how node/npm is supposed to work.

Resources