git add . command no response when adding node_modules - node.js

I have run
git init
npm install --save-dev webpack
Then, there is a package.json and a node_modules directory in the root directory.
I want to add the node_modules directory into git repo.
After I run
git add .
There is no response any more. And I found the git.exe process occupy more and more memory usage , but there is not any response after several hours.
What is wrong with it?
Is it caused by pretty much files to be added into repo by git?
How can I debug what happens in detail when that command is executed?
Everything works fine if I just git add some other files/folder which is not node_modules.

Have you git init the repo folder? If yes, then you can run git status. There you should see any new or changed folder.
If node_modules is not mentioned, I guess you have a .gitignore file in your project folder. If you want to git add the node modules folder you will need to remove node_modules/ from .gitignore.
npm install --save-dev webpack will just add one more dependency in package.json and install webpack in your node local repo. This is not a git related command.
Check also this question: Git - Ignore node_modules folder everywhere
Hopfully this will help,

I figured out myself.
This is caused by pretty much symlinks in the node_modules directory. If i evaluated the git-bash as administrator, the new installed node_module symlinks can be created as linux. And then when git add works well. Maybe, if without evaluated git-bash, git-bash can not create symlinks which cause pretty much files need to be git added, even worse maybe a circular reference between symblink and node module staff (not sure), that cause memory consumption of git increase always and no response.
Hope it can help others if met the same problem on windows.

Related

Workflow to checkout single node package for developing a patch / pull request

I want to add a feature to https://github.com/opentripplanner/otp-react-redux/ which is pulled in from the https://github.com/opentripplanner/otp-ui/tree/master/packages/geocoder package (add another geocoder).
Coming from the PHP world and composer, I normally do in such cases
composer install
rm -r vendor/foo/bar
composer install --prefer-source
cd vendor/foo/bar
git remote set-url origin <myforkURL>
git checkout main
Now I can easily edit that package in-place and make a pull request.
My question is: Is there a similar work-flow possible for node packages using yarn?
I already tried
yarn add "#opentripplanner/geocoder#master"
but no .git folder appeared in otp-react-redux/node_modules/#opentripplanner or otp-react-redux/node_modules/#opentripplanner/geocoder
Also it looks like that multiple packages are created from the #opentripplanner repo, which might complicate things.
I could try to simply edit the files in node_modules and then copy them to the a manually checked-out git repository, but when running yarn start everything is also overwritten.
EDIT: As the packages come from a monorepo I tried to delete all the #opentripplanner lines from packages.json and added:
yarn add opentripplanner/otp-ui#main
This now causes the build to fail.
I noticed, that the base package.json requires different package versions from the monorepo, so it will not work to require the complete the full main branch.
EDIT2: I found some clue here:
https://github.com/opentripplanner/otp-ui#development
but that also caused dependencies to not resolve properly.
EDIT3: yarn link actually looked promissing:
cd ..
git clone https://github.com/opentripplanner/otp-ui
cd otp-ui/packages/geocoder
yarn link
Now in the main project code (otp-react-redux)
yarn link "#opentripplanner/geocoder"
This creates a symlink in the node_modules folder to the specific folder in the monorepo I have cloned.
Unfortunately the build does not work:
Module not found: Can't resolve '#opentripplanner/geocoder' in 'otp-react-redux/lib/actions'
I even tried to match the version which is used in the main project by checking out the revision of 1.2.1
yarn link does the job!
cd ..
git clone https://github.com/opentripplanner/otp-ui
cd otp-ui
yarn
cd packages/geocoder
yarn link
Now in the main project code (otp-react-redux)
yarn link "#opentripplanner/geocoder"
This creates a symlink in the node_modules folder to the specific folder in the monorepo I have cloned.
To make the build work, the important part is that we run yarn in the monorepo before!
EDIT: Unfortunately the link process needs to be repeated for each of the #opentripplanner modules which require geocoder:
cd node_modules
$ find -name geocoder -type d
./trip-details/node_modules/#opentripplanner/geocoder
./vehicle-rental-overlay/node_modules/#opentripplanner/geocoder
./transitive-overlay/node_modules/#opentripplanner/geocoder
./endpoints-overlay/node_modules/#opentripplanner/geocoder
./zoom-based-markers/node_modules/#opentripplanner/geocoder
./trip-viewer-overlay/node_modules/#opentripplanner/geocoder
./trip-form/node_modules/#opentripplanner/geocoder
./transit-vehicle-overlay/node_modules/#opentripplanner/geocoder
./itinerary-body/node_modules/#opentripplanner/geocoder
./icons/node_modules/#opentripplanner/geocoder
./route-viewer-overlay/node_modules/#opentripplanner/geocoder
./printable-itinerary/node_modules/#opentripplanner/geocoder
./stop-viewer-overlay/node_modules/#opentripplanner/geocoder
./stops-overlay/node_modules/#opentripplanner/geocoder
./location-field/node_modules/#opentripplanner/geocoder
./park-and-ride-overlay/node_modules/#opentripplanner/geocoder
cd trip-details
yarn link "#opentripplanner/geocoder"
repeat for each of them until they are all links:
otp-react-redux$ find node_modules/ -name geocoder -type l
node_modules/#opentripplanner/trip-details/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/vehicle-rental-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/transitive-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/endpoints-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/zoom-based-markers/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/trip-viewer-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/trip-form/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/transit-vehicle-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/itinerary-body/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/icons/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/route-viewer-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/printable-itinerary/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/stop-viewer-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/stops-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/location-field/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/park-and-ride-overlay/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/base-map/node_modules/#opentripplanner/geocoder
node_modules/#opentripplanner/geocoder
yalc seems a good solution for this kind of problem
cd ~/projects/otp-ui/packages/itinerary-body
yarn tsc
yalc publish
cd ~/projects/otp-react-redux
yalc link #opentripplanner/itinerary-body
Now each time you change something in the package:
cd ~/projects/otp-ui/packages/itinerary-body
yarn tsc && yalc publish
cd ~/projects/otp-react-redux
yalc update
yarn start

how can I avoid npm install eachtime I change branch?

I have a git repository setup in git lab. Right now each time I change branch i should do:
npm install && composer install && cp .env.example .env && artisan generate key
Cause I lose .env , node_modules and composer modules. and it takes long time reinstalling them. cause I cant run it and test the branch if I dont have node_modules and other stuff installed
I wonder if Im doing something wrong or if there is a way to make it happen.
I have done lots of search but no luck.
Thanks in advance
Are you sure the files / directories you are talking about are ignored by git (they are in your .gitignore file)? If that's not the case, here is the answer to your question:
Since they are bound to the environment you are working on, they should not be touched by git by any means. That's why you should not lose them if you checkout on another branch.
Only the composer.lock, the package-lock.json and the .env.example should be versioned. Then, when you clone the repo from GitLab, you do a npm install, a composer install, you copy the .env.example etc... in order to setup your dependencies, but the dependencies directories (eg. node_modules) should not come from your repository.
Then after a while, let's imagine you want to update your Composer dependencies. You'll do a composer update. Your composer.lock file will be updated and will be committed to your repository.
Then, if somebody on another computer pulls your changes, he will only pull the newly updated composer.lock file. He will then make a composer install, which will install (or update if he already had installed them before) the dependencies from the composer.lock into his vendor folder.
I hope it helps you, feel free to ask more details in the comments :)

Pushing create-react-app to github node_modules folder has not been copied

I created an new folder my-app by running npx create-react-app my-app in node.js command. Once the folder was created, I wanted to upload the whole thing to my repo in my github account, so I opened git bash and run the commands to push all the files and folders to the new repo I created. After updating the repo, I found that all the files have been copies expect for the node_modules folder. I actually tried to run the npx create-react-app command in git bash command window, but failed. So I have to do it in node.js first then run the commands in git bash. Additionally, the repo was created without read me file. Did I do anything wrong here? why the folder was left behind. Can anyone help.
This is because create-react-app is bootstraped and comes with file .gitignore.
In which
node_modules are ignore
Why are they ignored because they are library which you can install any time by using npm install based on package.json file.
In package.json, there is section of dependencies, which are your node_modules. and npm install will make them available.
"dependencies": {
"bootstrap": "^4.1.3",
"fullcalendar-reactwrapper": "^1.0.7",
"moment": "^2.22.2",
"react": "^16.5.2",
"react-date-picker": "^7.0.0",
}
Git - Ignore node_modules folder everywhere
Nothing wrong with that, create-react-app has a lot of features and one of them is the automatic creation of a .gitignore file. If you check in your working directory, at the root there is that file.
Basically what this file does is telling to git what to track and what to ignore, it is common to totally ignore the node_modules folder because when you clone your repo, you'll just need your package.json to npm install your dependencies, so there is absolutely no need to upload a lot of data (node_modules could weight a lot).
gitignore docs
the /node_modules folder is in the .gitignore file created by create-react-app.
If you remove that entry from the .gitignore folder youdshould be able to commit the node_modules.
The create-react-app comes with a file called .gitignore. This file ensures unnecessary files are not committed to git. This is the reason your node_modules aren't being committed and pushed to github.
The main reason node_modules is included in the .gitignore is it's not necessary to push the file to the github.
you can get the node_modules folder whenever from package.json file of your project.
The second reason is the node_modules folder is large. Pushing it to github is a negative point.
If you want to get node_modules folder from a package.json file. You
just need to run the following commands from root of the project
directory when this package.json is present
if using yarn -
$ yarn install
if using npm -
$ npm install

Track yarn's node_modules directory properly in git

We've got a weird dependency in our CI that requires our git repo to have a node_modules directory (but obviously not the packages in it).
Since git doesn't track empty directories, the way we achieve it is to have a .gitignore inside:
node_modules/
.gitignore
With the following configuration
*
!.gitignore
This works, until we re-install our modules using yarn install, which will clear everything in the directory, including our .gitignore file, messing up the repository.
We can also add something like:
node_modules/
.empty
.gitignore
With .gitignore:
/node_modules/*
!/node_modules/.empty
But yarn install will still remove the file, and any tracking of the node_modules directory.
Our question, is there anything that we're missing that can:
Allow us to track node_modules/ in git
Won't break whenever we yarn install
I'm aware the actual answer is to fix our CI, but that's a bit out of scope for us right now.
Create a post install script that recreates the .empty file in node_modules.
More specifically, you can have certain scripts added in your package.json that run after install is called. In this case:
{
// ...
"scripts": {
// ...
"postinstall": "touch node_modules/.empty"
}
// ...
}
Should make sure there is a file in node_modules named .empty whenever install has completed.
Maybe you can prevent deletion of the "placeholder" file if you remove the write permission from the file for the user running yarn install. But maybeyarn install fails if it cannot clear the directory.

Can't make git stop tracking package-lock.json

With npm v5 here is now package-lock.json file being created after npm install
It is recommended to commit this file, but I have issue that this file is different for some reason between my dev machine and my server. Even if I push that file to repo, after npm install on server, file changes.
So now I'm attempting to make git untrack this file. After following numerous answers from other questions, I seem to have almost managed to do so, it's not tracked on dev machine, doesn't appear in the repo itself, but after I pull code to server and make npm install, it appears in modified files.
File is in .gitignore, but server git for some reason ignores it.
git check-ignore -v -n package-lock.json
:: package-lock.json
git check-ignore -v -n --no-index package-lock.json
.gitignore:10:package-lock.json package-lock.json
Possibly relevant info:
Dev machine: Windows 10.
Server: Ubuntu 14.04.
I'm pulling code to server using tags.
You need to remove it from your repo (git rm package-lock.json) in order for git to stop tracking it.
.gitignore only works for untracked files. If you have a tracked file that is also in your .gitignore, the fact that the file is tracked overrides the fact that it is also in .gitignore.

Resources