开发依赖与 node.js 中的依赖
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39716447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dev dependencies vs dependencies in node.js
提问by Maria Jane
In a node project I found 2 kind of dependencies:
在一个节点项目中,我发现了两种依赖项:
"dependencies": {
"axios": "0.9.1",
"express": "4.13.4",
"lodash": "4.6.1",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.4.0",
"react-router": "^2.0.0",
"redux": "^3.3.1"
},
"devDependencies": {
"babel-core": "^6.5.2"
}
I know the author install it via npm install babel-core --save -dev
我知道作者通过安装它 npm install babel-core --save -dev
but what is that for? when you push your code, the devDependenciesmodule is still there.
但那是为了什么?当你推送你的代码时,devDependencies模块仍然存在。
回答by Daniel Tonon
I wrote an article about this however it was taken down.
我写了一篇关于这个的文章,但它被删除了。
Snippets from the article:
文章摘录:
mod-a
dev-dependents:
- mod-b
dependents:
- mod-c
mod-d
? dev-dependents:
- mod-e
dependents:
- mod-a
----
npm install mod-d
installed modules:
- mod-d
- mod-a
- mod-c
----
checkout the mod-d code repository
npm install
installed modules:
- mod-a
- mod-c
- mod-e
Publishing to npm
发布到 npm
If you are publishing to npm, then it is important that you use the correct flag for the correct modules. If it is something that your npm module needs to function, then use the "--save" flag to save the module as a dependency. If it is something that your module doesn't need to function but it is needed for testing, then use the "--save-dev" flag.
如果您要发布到 npm,那么为正确的模块使用正确的标志很重要。如果您的 npm 模块需要运行它,请使用“--save”标志将该模块保存为依赖项。如果您的模块不需要运行但测试需要它,请使用“--save-dev”标志。
# For dependent modules
?npm install dependent-module --save
?# For dev-dependent modules
np?m install development-module --save-dev
Not for npm
不适用于 npm
If you aren't publishing to npm, it technically doesn't matter which flag you use. However, I find it a good practice to use the "--save" flag for modules that introduce non-standard code into the source files. Then use the "--sav-dev" flag for modules that are required for your compiler to function.
如果您不发布到 npm,那么从技术上讲,您使用哪个标志并不重要。但是,我发现对将非标准代码引入源文件的模块使用“--save”标志是一个很好的做法。然后对编译器运行所需的模块使用“--sav-dev”标志。
# For modules that introduce non-standard source code
npm install source-module --save
?# For modules that your compiler needs to function
np?m install compiler-module --save-dev
回答by Nitin Nema
Main difference between the two is:
两者的主要区别在于:
-in devdependencies, developer customize or modify the node package according to the requirement. For example while making grunt task we change the task as per requirement in Gruntfile, same as case with babel you are using.
- 在 devdependencies 中,开发者根据需求自定义或修改 node 包。例如,在制作 grunt 任务时,我们根据 Gruntfile 中的要求更改任务,与您使用的 babel 的情况相同。
-in dependencies, developer directly use the node package without the change ex-express.
-in 依赖,开发者直接使用 node 包,无需更改 ex-express。
Hopefully it clears your doubt.
希望它能消除你的疑惑。
回答by Sithija Piyuman Thewa Hettige
Dependencies vs dev dependencies
依赖与开发依赖
Dev dependencies are modules which are only required during development whereas dependencies are required at runtime. If you are deploying your application, dependencies has to be installed, or else your app simply will not work. Libraries that you call from your code that enables the program to run can be considered as adependencies.
开发依赖项是仅在开发期间需要的模块,而在运行时需要依赖项。如果您正在部署应用程序,则必须安装依赖项,否则您的应用程序将无法运行。您从使程序运行的代码中调用的库可以被视为依赖项。
Eg- React , React - dom
例如-反应,反应-dom
Dev dependency modules need not be installed in the production server since you are not gonna develop in that machine .compilers that covert your code to javascript , test frameworks and document generators can be considered as dev-dependencies since they are only required during development .
开发依赖模块不需要安装在生产服务器中,因为你不会在那台机器上开发。将代码转换为 javascript 的编译器,测试框架和文档生成器可以被视为开发依赖,因为它们只在开发过程中需要。
Eg- ESLint , Babel , webpack
例如- ESLint , Babel , webpack

