bower和npm的依赖管理

npm 是Node.js 模块管理的标准工具, 同时也可以结合诸如Browserify等其他工具, 用以管理前端.
Bower 是用于前端依赖管理的工具, 并为前端做了不少的优化.
两者的本质区别, npm 支持nested dependency tree.

project root
[node_modules] // default directory for dependencies
 -> dependency A
 -> dependency B
    [node_modules]
    -> dependency A

 -> dependency C
    [node_modules]
    -> dependency B
      [node_modules]
       -> dependency A 
    -> dependency D 

而bower 只支持flat dependency tree.

project root
[bower_components] // default directory for dependencies
 -> dependency A
 -> dependency B // needs A
 -> dependency C // needs B and D
 -> dependency D

nested dependency tree 避免了依赖冲突, 但耗费了更多的空间和时间. 这在前端项目中是不可接受的, 例如它很可能会下载多个jquery 的copy.
而bower 为最小的资源加载而优化. 同一个包, 只会在依赖关系中出现一次. 所以可能会有潜在的依赖冲突问题的产生. 例如A 和B 都依赖于C, 但A 依赖的是C 的V1 版本, 而B 依赖的是V2 版本, 这在bower 中会参数冲突, 同一资源(包) 不能有多个副本!

通常, Bower 会在webpage 发布时使用, 因为此时必须避免duplication. 而npm 会在testing, building, optimizing, checking 等开发阶段中被使用.

你可能感兴趣的:(bower和npm的依赖管理)