有什么区别:
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关键字的真正作用是什么?
当前回答
让我给你们举个例子,
你是一个非常严肃的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.
其他回答
这里所有的解释都很棒,但缺少一个非常重要的东西:如何仅安装生产依赖项?(没有开发依赖项)。 我们通过使用——save或——save-dev将依赖项与devDependencies分开。 安装所有我们使用的:
npm i
要只安装生产包,我们应该使用:
npm i --only=production
人们在生产中使用npm来做一些非常酷的事情,Node.js就是一个例子,所以你不希望所有的开发工具都在运行。
如果您正在使用gulp(或类似)来创建构建文件并将其放到服务器上,那么这并不重要。
——save-dev将semver规范保存到包描述符文件中的“devDependencies”数组中,——save将其保存到“dependencies”数组中。
——save-dev用于保存用于开发目的的包。 例如:单元测试、简化… ——save用于保存 应用程序运行所需的包。
已经给出了明确的答案。但是值得一提的是devDependencies是如何影响安装包的:
默认情况下,npm install将安装package中列出的所有依赖项。json。使用——production标志(或者当NODE_ENV环境变量被设置为production时),npm将不会安装devDependencies中列出的模块。
参见:https://docs.npmjs.com/cli/install