有什么区别:
npm install [package_name]
and:
npm install [package_name] --save
and:
npm install [package_name] --save-dev
这是什么意思?——save和-dev关键字的真正作用是什么?
有什么区别:
npm install [package_name]
and:
npm install [package_name] --save
and:
npm install [package_name] --save-dev
这是什么意思?——save和-dev关键字的真正作用是什么?
当前回答
正如@andreas-hultgren在这个回答中所建议的,根据npm文档:
如果有人打算下载并在他们的程序中使用您的模块,那么他们可能不想或不需要下载并构建您使用的外部测试或文档框架。
然而,对于web应用程序开发,Yeoman(一个脚手架工具,用于安装同行评审的预先编写的包)。将所有的包放在devDependencies中,而不是放在dependencies中,所以看起来——save-dev的使用在web应用程序开发中是一个安全的赌注,至少。
其他回答
一个完美的例子是:
$ npm install typescript --save-dev
在这种情况下,你会希望使用Typescript(一种可解析javascript的编码语言)进行开发,但一旦应用被部署,就不再需要Typescript了,因为所有的代码都已转译为javascript。因此,将它包含在已发布的应用程序中毫无意义。事实上,它只会占用空间并增加下载时间。
基本上我们写
npm install package_name
但特别为了测试目的,我们不需要在应用程序运行在正常状态下运行一些包,因此Node引入了很好的方法来解决这个问题。每当我们写作时
npm install package_name --save-dev
当时,该包仅用于开发目的而安装。
人们在生产中使用npm来做一些非常酷的事情,Node.js就是一个例子,所以你不希望所有的开发工具都在运行。
如果您正在使用gulp(或类似)来创建构建文件并将其放到服务器上,那么这并不重要。
——save-dev将semver规范保存到包描述符文件中的“devDependencies”数组中,——save将其保存到“dependencies”数组中。
让我给你们举个例子,
你是一个非常严肃的npm库的开发者,它使用不同的测试库来测试包。 用户下载您的库,并希望在他们的代码中使用它。他们也需要下载您的测试库吗?也许你用笑话来测试,而他们用摩卡。你想让他们也安装笑话吗?只是为了管理你的图书馆?
不。对吧?这就是为什么它们在devDependencies中。
当有人这样做时,npm i yourPackage只会安装运行你的库所需的库。你用来捆绑代码或测试和模拟的其他库将不会被安装,因为你把它们放在了devDependencies中。很简洁,对吧?
那么,为什么开发人员需要公开devdependencies呢?
Let's say your package is an open-source package and 100s of people are sending pull requests to your package. Then how they will test the package? They will git clone your repo and when they would do an npm i the dependencies as well as devDependencies. Because they are not using your package. They are developing the package further, thus, in order to test your package they need to pass the existing test cases as well write new. So, they need to use your devDependencies which contain all the testing/building/mocking libraries that YOU used.