Why is the node_modules folder not committed to Git?

泪湿孤枕 提交于 2019-12-08 14:46:33

问题


When I create an AngularJS project with

yo angular 

it creates a node_modules/ directory but puts that directory in .gitignore. When I clone the project elsewhere and run

grunt server

I get

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt hasn't been installed locally to your project. For more information about installing and configuring grunt, please see the Getting Started guide:

The getting started guide doesn't say anything about how to handle the missing node_modules/ directory however.

The node_modules/ directory is missing deliberately because yeoman put it in .gitignore.

What's the right way to use Yeoman and Grunt in this case?
Is there some better documentation for Yeoman and Grunt?

Thanks.


回答1:


That is the correct behaviour. The contents of node_modules is not meant to be committed to source control. The idea behind npm is that it tracks all required node dependencies in a file called package.json. This file should be committed to source control. Then on a different machine, you can check out the code and run

npm install

This looks at the package.json file and downloads all required files from the cloud and puts them all into a new node_modules directory.




回答2:


If you do enough searching on the topic you'll eventually run across the following article which states that if you're building an application that you should check-in your dependencies. Reliance on package.json can cause issues as module authors publish updates, a better solution is to use

npm shrinkwrap

which creates a file locking down your dependencies and your dependencies dependencies but even this can be brittle as it is possible for a module author to re-publish the same version with different code.

Since yo angular is creating an application IMHO node_modules should not be included in .gitignore, however as I just rudely discovered if you're on Windows there's a significant chance that you can't check-in the modules in that folder due to path lengths...sigh.



来源:https://stackoverflow.com/questions/19328613/why-is-the-node-modules-folder-not-committed-to-git

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!