有什么区别:
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关键字的真正作用是什么?
当前回答
一般来说,您不希望产品包中充斥着只打算用于开发目的的东西。
使用——save-dev(或-D)选项来分离单元测试框架(jest, jasmine, mocha, chai等)等包。
你的应用需要用于生产的任何其他包都应该使用——save(或-S)来安装。
npm install --save lodash //prod dependency
npm install -S moment // " "
npm install -S opentracing // " "
npm install -D jest //dev only dependency
npm install --save-dev typescript //dev only dependency
如果你打开包裹。Json文件,然后你会看到这些条目列出在两个不同的部分:
"dependencies": {
"lodash": "4.x",
"moment": "2.x",
"opentracing": "^0.14.1"
},
"devDependencies": {
"jest": "22.x",
"typescript": "^2.8.3"
},
其他回答
——save-dev将semver规范保存到包描述符文件中的“devDependencies”数组中,——save将其保存到“dependencies”数组中。
这里所有的解释都很棒,但缺少一个非常重要的东西:如何仅安装生产依赖项?(没有开发依赖项)。 我们通过使用——save或——save-dev将依赖项与devDependencies分开。 安装所有我们使用的:
npm i
要只安装生产包,我们应该使用:
npm i --only=production
正如@andreas-hultgren在这个回答中所建议的,根据npm文档:
如果有人打算下载并在他们的程序中使用您的模块,那么他们可能不想或不需要下载并构建您使用的外部测试或文档框架。
然而,对于web应用程序开发,Yeoman(一个脚手架工具,用于安装同行评审的预先编写的包)。将所有的包放在devDependencies中,而不是放在dependencies中,所以看起来——save-dev的使用在web应用程序开发中是一个安全的赌注,至少。
as——save是npm的默认选项,所以我使用
npm i package
还有,我用的是
npm i package -D
默认选项将安装包作为项目依赖项,其中as -D为开发依赖项,如测试,lint等,并为开发过程安装包
你可以在这里找到所有的标志https://docs.npmjs.com/cli/v8/commands/npm-install
让我给你们举个例子,
你是一个非常严肃的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.