有什么区别:
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只是在node_modules下安装一个包。当你试图为你的app/模块安装依赖项时,你需要先安装它们,然后将它们添加到package.json的依赖项部分。
——save-dev将第三方包添加到包的开发依赖项中。当有人直接运行npm install来安装你的包时,它不会被安装。通常只有当有人先克隆你的源存储库,然后在其中运行npm install时才会安装它。
——save将第三方包添加到包的依赖项中。当有人运行npm install package时,它将与包一起安装。
开发依赖项是那些只在开发包时需要的依赖项。这可以包括测试运行器、编译器、打包器等。 这两种类型的依赖关系都存储在包的包中。json文件。——save-dev添加到devDependencies
NPM安装文档可以参考这里。
--
请注意——save现在是默认选项,从NPM 5开始。因此,不再显式地需要它。不使用——save命令也可以运行npm install来达到同样的效果。
其他回答
当你使用npm install <package-name>安装一个npm包时,你将它作为一个依赖项安装。
该包将自动列在包中。Json文件,在依赖项列表下(从NPM 5开始:在你必须手动指定-save之前)。 例:NPM安装lodash 按回车键后检查您的包。json文件。
"dependencies": {
"lodash": "4.x",
},
当您添加-D标志或——save-dev时,您正在将其作为开发依赖项安装,这将其添加到devDependencies列表中。
示例:NPM install——save-dev lite-server 按回车键后检查您的包。json文件
"devDependencies": {
"lite-server": "^2.6.1"
},
开发依赖关系是仅用于开发的包,在生产环境中不需要。例如测试包、webpack或Babel。
当你进入生产环境时,如果你输入npm install并且文件夹中包含一个包。Json文件,它们被安装,因为NPM假设这是一个开发部署。
你需要设置——production标志(npm install——production)来避免安装那些开发依赖项。
——save-dev用于应用程序开发中使用的模块,而不是在生产环境中运行时需要的模块 ——save用于将其添加到包中。Json,它是运行应用程序所必需的。
例如:express,body-parser,lodash,helmet,mysql所有这些都是在运行应用程序时使用的,而mocha,istanbul,chai,sonarqube-scanner都是在开发过程中使用的,所以把它们放在dev-dependencies中。
NPM link或NPM install也会在你的项目文件夹中安装开发依赖模块和依赖模块
阅读完成并忘记——保存开发头痛
最简单的答案是——当你为其他开发人员创建包,并希望将包托管在NPM Registry(如lodash、mongoose、express等)时,save-dev非常有用。当你构建或编写Node Server时,——save和——save-dev之间没有区别,因为你的Node Server实现对你来说是私有的,你永远不会在NPM上发布它。
NPM安装如何工作
Whenever we install a new package using npm like npm install express then NPM installs that package to our system and put it into node_modules folder, now NPM will analyze the package.json file of newly installed package i.e express in this case, after analyzing NPM will install all those packages which were mentioned in dependencies section of package.json file of express package. After installing those packages on which express was dependent NPM again analyze the package.json file of all newly installed packages and again install the packages for them, this cycle goes on until all packages are available into node_modules folder to function properly. You can check package dependencies by running npm list in terminal where terminal should point location of your project directory.
——save-dev如何与上述解释的内容相关
Suppose you want to create a new package like express, now while development of this new package you probably want to write some unit testing code and test the package with any other available testing package let's assume mocha in this case. Now you know mocha is only required to test the package not required to use the package. In this case you should install mocha using --save-dev flag, otherwise NPM will install it whenever a developer install your package using NPM. So if we want a dependency not installed when someone install our package from NPM we must install that package using --save-dev in development phase.
最后一件事
不要把——save-dev和协作开发混在一起,如果有人从像github这样的源版本控制系统中克隆了你的包代码,那么NPM肯定会安装所有的devDependencies,也就是使用——save-dev安装的包。
如果您在自己的项目中尝试过——save和——save-dev,那么它们之间的区别可能不会立即被注意到。这里有几个例子…
假设您正在构建一个应用程序,该应用程序使用moment包来解析和显示日期。你的应用程序是一个调度程序,所以它确实需要这个包来运行,就像:没有它就不能运行。在这种情况下,你会使用
npm install moment --save
这将在package.json中创建一个新值
"dependencies": {
...
"moment": "^2.17.1"
}
在开发时,使用测试套件等工具确实有帮助,可能需要jasmine-core和karma。在这种情况下,你会使用
npm install jasmine-core --save-dev
npm install karma --save-dev
这也会在package.json中创建一个新值
"devDependencies": {
...
"jasmine-core": "^2.5.2",
"karma": "^1.4.1",
}
您不需要测试套件来运行应用程序的正常状态,所以它是一个——save-dev类型的依赖,仅此而已。你可以看到,如果你不理解到底发生了什么,那就有点难以想象。
直接从NPM docs docs#dependencies中获取
依赖关系 依赖项在映射包名的简单对象中指定 到版本范围。版本范围是一个字符串,包含一个或 更多空格分隔的描述符。依赖关系也可以被识别 使用tarball或git URL。 请不要将测试工具或转译器放在您的依赖项中 对象。参见下面的devDependencies。
即使在文档中,它也会要求你使用——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.