DEV Community

Cover image for Automatically Install NPM Dependencies on Git Pull

Automatically Install NPM Dependencies on Git Pull

Chris Cook on December 27, 2021

UPDATE: I released a npm package for this script: git-pull-run. Please report any issues or suggestions to improve on GitHub. Together with my t...
Collapse
 
sso profile image
Sall •

Nice one. I am looking for solutions on how to improve:

Your opinions, suggestions would be highly appreciated.

Collapse
 
prabhakarnikhil14 profile image
prabhakarnikhil14 •

Can't we just run npm install on each pull

Collapse
 
zirkelc profile image
Chris Cook •

You mean always running install without checking for lock file changes? Of course that’s an option. I’m using npm’s postinstall lifecycle hook to set up the dev environment (generate code, deploy changes, etc.). So avoiding unnecessary installs is important to me.

Collapse
 
gaardsholt profile image
Lasse Gaardsholt •

Shouldn't you run npm ci instead of npm install ?
Won't npm install try to update your package.json?

Collapse
 
zirkelc profile image
Chris Cook •

If the semver versions in package.json and package-lock.json fit with each other, npm install should behave identically to npm ci.

For more information check this comment by the creator of npm ci: dev.to/zkat/comment/epbj

Collapse
 
aybee5 profile image
Ibrahim Abdullahi Aliyu •

This is pretty cool, can it be a package one can just install?

Collapse
 
zirkelc profile image
Chris Cook •

I’m working on a npm package :)

Collapse
 
zirkelc profile image
Chris Cook •

I released a first version: npmjs.com/package/git-pull-run

I'd like to hear your thoughts! :-)

Collapse
 
felipekm profile image
Kautzmann •

Hi there, nice article!
Do we need to setup this husky hook into package.json?

Collapse
 
zirkelc profile image
Chris Cook •

No, Husky in the latest version v7 doesn't work npm scripts anymore. Just follow these steps and it create a .husky folder in your repo. Then add a post-merge file with gist snippet above.

Collapse
 
arpitprod profile image
arpitprod •

If we are using yarn then How to do same thing ?

Collapse
 
zirkelc profile image
Chris Cook •

That should be easy with changing the regex expression from package-lock.json to yarn.lock: PACKAGE_LOCK_REGEX="(^packages\/.*\/yarn\.lock)|(^yarn\.lock)" and running yarn install instead of npm install inside the hook.

Collapse
 
lamianbu profile image
Collapse
 
sushruth profile image
Sushruth Shastry •

Good point! Another way we are solving it at work is by using yarn pnp with zero installs. It's not bulletproof but surely helps a lot.

Collapse
 
zirkelc profile image
Chris Cook •

I’d like to use yarn pnp but it’s not widely supported yet. For example Typescript doesn’t support pnp at the moment (it’s being patched into TS by yarn during install). Also bundlers like webpack didn’t handle the lock file at root level correctly the last time I checked.