网上对于这两个的区别解释都是统一口径的, 一个是开发依赖, 一个是线上依赖, 打包发布需要用到的要添加到线上依赖, 一模一样的回答, 误导了很多人. 今天自己测试一下这两个命令, 记录一下.
--save-dev, 会在 devDependencies 里面添加依赖
-D, 会在 devDependencies 里面添加依赖
--save, 会在 dependencies 里面添加依赖
-S, 会在 dependencies 里面添加依赖
devDependencies 和 dependencies 可以同时存在同一个包的依赖.
如果 NPM install xxx 后面没有输入要保存到哪个里面, devDependencies 和 dependencies 都没有.
我这边直接 NPM install jQuery,node_modules 下有 jQuery. 然后我删除 node_modules, 执行 NPM install,node_modules 下并没有下载 jQuery.
所以, 安装依赖的时候如果没有加上要依赖到开发还是线上, 只是临时的在 node_modules 里面帮你下载, 而 devDependencies 和 dependencies 的依赖都会帮你下载.
然后我在 devDependencies 下安装依赖:
在入口文件引用和打印 jQuery:
- import $ from 'jquery'
- console.log($)
打包之后, 可以使用 jQuery.
然后我在 dependencies 下安装依赖:
- "dependencies": {
- "html-webpack-plugin": "^4.0.3",
- "jquery": "^3.4.1",
- "webpack": "^4.42.1",
- "webpack-cli": "^3.3.11"
- }
在入口文件引用和打印 jQuery:
- import $ from 'jquery'
- console.log($)
打包之后, 可以使用 jQuery.
测试的结果就是, 无论是 --save 还是 --save-dev, 对于打包都没有任何影响. devDependencies 和 dependencies 两种情况, 打包出来的 main.JS 都把 jQuery 打包进去. 这两种情况, 如果都没有引用 jQuery 的情况下, 也都不会把 jQuery 打包.
接着在一个空白的项目里面下载 axios,NPM install axios -S, 打开 node_modules 文件夹:
image
发现多出了另外三个依赖, 查看 axios 下的 package.JSON:
- "dependencies": {
- "follow-redirects": "1.5.10"
- }
查看 follow-redirects 下的 package.JSON:
- "dependencies": {
- "debug": "=3.1.0"
- }
查看 debugs 下的 package.JSON:
- "dependencies": {
- "ms": "2.0.0"
- }
最后 ms 的 package.JSON 没有 dependencies.
而这几个包的 devDependencies 依赖的包没有一个下载.
接着我在 node_modules 把 follow-redirects,debugs,ms 都删了, 把 axios 里面的 package.JS 的 dependencies 给删了, 然后执行 NPM install, 发现没有下载 follow-redirects,debugs,ms 这几个, 也证明了如果 node_modules 里面有下载的包, 是不会重新去下载的. 我把 node_modules 删除, 执行 NPM install, 这几个包又都下载下来了.
最后得出 的结论是,--save-dev 和 --save 在平时开发的时候, 对于打包部署上线是没有任何影响的. 如果你是发布一个包给别人用, 而你开发的包依赖第三方的包, 那么你如果是 --save, 那么别人安装你开发的包, 会默认下载你依赖的包, 如果你是 --save-dev, 那么别人安装你开发的包, 是不会默认帮忙下载你依赖的包.
其实发布的包如果没有必要, 很少会默认帮你下载, 比如 Bootstrap, 依赖 jQuery, 怕你原本就下载了引起冲突, 也不会在 dependencies 里面安装 jQuery 而是:
- "peerDependencies": {
- "jquery": "1.9.1 - 3",
- "popper.js": "^1.16.0"
- }
表示 Bootstrap 依赖于这两个包, 你必须安装, 版本不固定, 但是一定要安装这两个包, 安装的时候会有警告:
- peerDependencies WARNING Bootstrap@* requires a peer of jQuery@1.9.1 - 3 but none was installed
- peerDependencies WARNING Bootstrap@* requires a peer of popper.JS@^1.16.0 but none was installed
当你引用了然后打包, 报错:
- ERROR in ./node_modules/_bootstrap@4.4.1@Bootstrap/dist/JS/Bootstrap.JS
- Module not found: Error: Can't resolve'jQuery'in'C:\Users\wade\Desktop\savedev\node_modules\_bootstrap@4.4.1@Bootstrap\dist\JS'
- @ ./node_modules/_bootstrap@4.4.1@Bootstrap/dist/JS/Bootstrap.JS 7:82-99
- @ ./src/index.JS
- ERROR in ./node_modules/_bootstrap@4.4.1@Bootstrap/dist/JS/Bootstrap.JS
- Module not found: Error: Can't resolve'popper.JS'in'C:\Users\wade\Desktop\savedev\node_modules\_bootstrap@4.4.1@Bootstrap\dist\JS'
- @ ./node_modules/_bootstrap@4.4.1@Bootstrap/dist/JS/Bootstrap.JS 7:101-121
- @ ./src/index.JS
以上就是对 --save 和 --save-dev 的一些测试, 想更快的得出结论其实是自己发布一个包. 至于本人的答案是不是存在错误, 欢迎指出, 因为只是自己简单测试的结果.
image
来源: http://www.jianshu.com/p/645218dba49d