Mishel Parkour
Mishel Parkour

Reputation: 629

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

Upvotes: 6

Views: 4877

Answers (1)

Anthony Aslangul
Anthony Aslangul

Reputation: 3847

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 :)

Upvotes: 4

Related Questions