有什么区别:
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关键字的真正作用是什么?
当前回答
我想补充一些我的想法
我认为当别人使用你的代码而不是你自己使用时,所有的差异都会出现
例如,您编写了一个称为节点请求的HTTP库
在你的图书馆里,
你使用lodash来处理字符串和对象,没有lodash,你的代码就不能运行
如果有人将您的HTTP库作为其代码的一部分使用。你的代码会和他的代码一起编译。
你的代码需要lodash,所以你需要放入依赖来编译
如果你写一个项目,比如monaco-editor,它是一个网络编辑器,
你已经使用webpack捆绑了你所有的代码和你的产品env库,当构建完成时,只有一个monaco-min.js
所以有人不关心是——save还是——save-dependencies,他只需要monaco-min.js
简介:
如果有人想编译你的代码(用作库), 将你的代码使用的lodash放入依赖项中 如果有人想在你的代码中添加更多的特性,他需要单元测试和编译器,把这些放到dev-dependencies中
其他回答
一个完美的例子是:
$ npm install typescript --save-dev
在这种情况下,你会希望使用Typescript(一种可解析javascript的编码语言)进行开发,但一旦应用被部署,就不再需要Typescript了,因为所有的代码都已转译为javascript。因此,将它包含在已发布的应用程序中毫无意义。事实上,它只会占用空间并增加下载时间。
——save-dev用于保存用于开发目的的包。 例如:单元测试、简化… ——save用于保存 应用程序运行所需的包。
——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.
——save-dev用于应用程序开发中使用的模块,而不是在生产环境中运行时需要的模块 ——save用于将其添加到包中。Json,它是运行应用程序所必需的。
例如:express,body-parser,lodash,helmet,mysql所有这些都是在运行应用程序时使用的,而mocha,istanbul,chai,sonarqube-scanner都是在开发过程中使用的,所以把它们放在dev-dependencies中。
NPM link或NPM install也会在你的项目文件夹中安装开发依赖模块和依赖模块