Track yarn's node_modules directory properly in git - node.js

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.

Related

why can't i hide the node_module file by .gitignore after applying node_modules/ or /node_modules on nodejs

My .gitignore file is on as the same root as .git folder
i have also tried out different format like 'node_modules/' '/node_modules'
though it's hiding my .env file.
But gitignore still not hiding my node_modules folder
I have also tried by uninstalling the node_modules > updated the .gitignore file > npm install. Still it doesn't work. What should i do now?
Try using node_modules/*
This will tell git to ignore all files and folders inside the node modules folder.
Also make sure to check it by adding a dependency and using the command
git status
It will give you a list of all changed files.
It may be because your git repository is already tracking your 'node_modules' folder. Try removing the folder and commiting the change, then reinstall your node modules with npm i. The folder should then remain untracked and will appear greyed out in VS Code.

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

git add . command no response when adding node_modules

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.

NPM Install, move files with a build script

I have built a custom npm package that is essentially a list of dependencies, a gulp file, and a configuration file for it. The idea is that I can run npm install <path-to-git-repo> to pull in gulp whenever I start a new development project.
However, I would like the gulp file and config file to be moved into the current directory, and not remain inside of node_modules. I have tried to accomplish this with the install script in my package.json file listed below, but it didn't seem to do anything. The files I'm trying to move are included in the files section of package.json.
"scripts":
{
"install": "mv gulpfile.js ./ && mv gulp.config.json ./"
},
Thanks for your help
For something like this I think it would be much easier to have 1 source gulp file and specify the dependent gulp file(s) wherever you're installing them. example blog
Also, just to point out, install is typically considered an anti-pattern
But to answer your exact question, this command mv gulpfile.js ./ && mv gulp.config.json ./ looks to me like you're saying move gulpfile.js (which is already in the root directory), into ./ (which is the same directory). Check your source directory actually contains the gulpfile.js by running the command yourself on the command line

Resources