且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

npm install vs sudo npm install -g

更新时间:2021-11-07 22:17:34

npm 在本地安装包,即.在当前文件夹内的 node_modules 文件夹中.这允许您的应用程序依赖于特定的软件包版本,而不必弄乱系统上已安装软件包的全局列表.见第一段Isaac 的博文(同时处理同一事物的多个版本),很好地解释了 npm 如何避免在其他编程生态系统中经常遇到的依赖地狱.

npm installs packages locally, ie. in a node_modules folder inside your current folder. This allows your application to depend on specific packages versions, without having to mess up with a global list of installed packages on your system. See the first paragraph of Isaac's blog post (Handle multiple versions of the same thing at the same time), which explains well how npm avoids the dependency hell often encountered in other programming ecosystems.

另一方面,一些包旨在用作命令行实用程序,例如 grunt-climochajson.为了在任何地方使用它们,您需要全局安装它们,因此使用 -g 参数.

On the other hand, some packages are meant to be used as command line utilities, such as grunt-cli, mocha or json. In order to use them everywhere, you need to install them globally, hence the -g parameter.

请注意,您不需要 sudo 来安装全局包,请参阅 此相关答案 了解更多信息.

Please note that you shouldn't need sudo to install global packages, see this relevant answer for more information.