有什么区别:

npm install [package_name]

and:

npm install [package_name] --save

and:

npm install [package_name] --save-dev

这是什么意思?——save和-dev关键字的真正作用是什么?


当前回答

——save-dev用于应用程序开发中使用的模块,而不是在生产环境中运行时需要的模块 ——save用于将其添加到包中。Json,它是运行应用程序所必需的。

例如:express,body-parser,lodash,helmet,mysql所有这些都是在运行应用程序时使用的,而mocha,istanbul,chai,sonarqube-scanner都是在开发过程中使用的,所以把它们放在dev-dependencies中。

NPM link或NPM install也会在你的项目文件夹中安装开发依赖模块和依赖模块

其他回答

——save-dev将semver规范保存到包描述符文件中的“devDependencies”数组中,——save将其保存到“dependencies”数组中。

人们在生产中使用npm来做一些非常酷的事情,Node.js就是一个例子,所以你不希望所有的开发工具都在运行。

如果您正在使用gulp(或类似)来创建构建文件并将其放到服务器上,那么这并不重要。

让我给你们举个例子,

你是一个非常严肃的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(生产依赖项) ——global或-g(全局使用,即可以在本地系统的任何地方使用)

当你使用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)来避免安装那些开发依赖项。