For example if I've switched branch with git and want to sync node_modules
with current package.json
. How do I do that?
Not committing node_modules implies you need to list all your modules in the package. json (and package-lock. json ) as a mandatory step. This is great because you might not have the diligence to do so, and some of the npm operations might break if you don't.
You can copy but it will be of no use, its as simple as copy the package. json entry for "react-native-fetch-blob" and do npm install. Also you can do "npm install react-native-fetch-blob@<version>" if you want a specific version else latest will be installed.
Simply install syncyarnlock, and execute with the options applicable to your needs. For example, to sync a project's package. json with the project's yarn. lock, and have the ranges remain intact while updating the versions to reflect what will actually be installed, simply run: syncyarnlock -s -k .
You can always copy node_modules and then run npm install or npm update in the new project to make sure you've got up-to-date versions. npm will use the files in node_modules as a cache and should only bring down newer content if required. In short: it won't hurt.
If your new branch has new npm packages or updated version dependencies, just run $ npm install
again after switching branches.
If your new branch removes npm packages from package.json, run $ npm prune
We can make use of git hooks to run the npm install
automatically when package.json changes when we pull or checkout to different branch.
Here is the script that needs to be executed. We basically check whether package.json file is present in the diff.
#/usr/bin/env bash
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
check_run package.json "npm install"
To run the above script on
chmod +x post-merge
to make it executable then mv post-merge .git/hooks/
put it into git hooks.chmod +x post-checkout
and then mv post-checkout .git/hooks/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With