Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check in node_modules vs. shrinkwrap

Tags:

node.js

npm

Checking in node_module was the community standard but now we also have an option to use shrinkwrap. The latter makes more sense to me but there is always the chance that someone did "force publish" and introduced a bug. Are there any additional drawbacks?

like image 409
Yaron Naveh Avatar asked Jul 12 '12 20:07

Yaron Naveh


People also ask

Should you check in node_modules?

You should not include folder node_modules in your . gitignore file (or rather you should include folder node_modules in your source deployed to Heroku). If folder node_modules: exists then npm install will use those vendored libraries and will rebuild any binary dependencies with npm rebuild .

Should you use npm shrinkwrap?

NPM shrinkwrap also helps you use same package versions on all environments (development, staging, production) and also improve download and installation speed. Having same versions of packages on all environments can help you test systems and deploy with confidence.

Should I put node_modules in Gitignore?

The default option is here not to commit the node_modules folder, you should instead add it to the . gitignore file.

What is node shrinkwrap?

NPM shrinkwrap is used to lock the dependency version in a project. After installing packages using npm install or npm install package-name and updating your node_modules folder, you should run npm shrinkwrap. It will create new npm-shrinkwrap.


1 Answers

My favorite post/philosophy on this subject goes all the way back (a long time in node.js land) to 2011:

https://web.archive.org/web/20150116024411/http://www.futurealoof.com/posts/nodemodules-in-git.html

To quote directly:

If you have an application, that you deploy, check in all your dependencies in to node_modules. If you use npm do deploy, only define bundleDependencies for those modules. If you have dependencies that need to be compiled you should still check in the code and just run $ npm rebuild on deploy.

Everyone I’ve told this too tells me I’m an idiot and then a few weeks later tells me I was right and checking node_modules in to git has been a blessing to deployment and development. It’s objectively better, but here are some of the questions/complaints I seem to get.

I think this is still the best advice.

The force-publish scenario is rare and npm shrinkwrap would probably work for most people. But if you're deploying to a production environment, nothing gives you the peace-of-mind like checking in the entire node_modules directory.

Alternately, if you really, really don't want to check in the node_modules directory but want a better guarantee there hasn't been a forced push, I'd follow the advice in npm help shrinkwrap:

If you want to avoid any risk that a byzantine author replaces a package you're using with code that breaks your application, you could modify the shrinkwrap file to use git URL references rather than version numbers so that npm always fetches all packages from git.

Of course, someone could run a weird git rebase or something and modify a git commit hash... but now we're just getting crazy.

like image 170
smithclay Avatar answered Sep 21 '22 08:09

smithclay