Why is the node_modules folder not committed to Gi

2019-06-15 01:10发布

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.

2条回答
疯言疯语
2楼-- · 2019-06-15 02:02

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.

查看更多
Animai°情兽
3楼-- · 2019-06-15 02:08

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.

查看更多
登录 后发表回答