简体   繁体   English

使用npm节点控制台或package.json安装模块有什么区别

[英]What the difference between install a module with npm node console or in package.json

I'm new and I need to use node for off-line use so, I'm trying to understand how the install modules work. 我是新手,我需要使用节点进行离线使用,所以,我试图了解安装模块的工作原理。

It's the same if I use npm install express or included it in the package.json ? 如果我使用npm install express或将其包含在package.json它是一样的吗?

The way a package is installed will be the same whether you manually type npm install express or put it in your package.json and then do npm install . 无论您手动键入npm install express还是将其放入package.json然后执行npm install包的方式都是相同的。 The difference comes when you try to install your Node project elsewhere. 当您尝试在其他位置安装Node项目时会出现差异。

For example, if your code was checked into GitHub and you didn't include a package.json with all of the dependencies listed, then when the project was downloaded you would have to manually re-install all of the dependencies on the command line in order for it to work. 例如,如果您的代码被检入GitHub并且您没有包含列出所有依赖项的package.json ,那么在下载项目时,您必须手动重新安装命令行中的所有依赖项。为了它的工作。 But if you had checked in a package.json with the code, then you could run npm install to install all of the dependencies at once, and not have to remember which ones were necessary. 但是如果您使用代码签入了package.json ,那么您可以运行npm install来立即安装所有依赖项,而不必记住哪些是必需的。

In addition, the package.json allows you to specify an "approximate version" of a dependency to use. 此外, package.json允许您指定要使用的依赖项的“近似版本”。 This way if a few packages in your project share a dependency and they all specify similar "approximate versions", only one version will be installed and it will be shared between packages. 这样,如果项目中的一些包共享一个依赖项,并且它们都指定了类似的“近似版本”,则只会安装一个版本,它将在包之间共享。 This saves some install time. 这节省了一些安装时间。

Nothing actually. 实际上没什么。 But you don't want to do that again and again. 但你不想一次又一次地这样做。 So you might as well put your module dependencies in your package.json 所以你不妨把你的模块依赖项放在package.json中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM