What is the fundamental difference between bower
and npm
? Just want something plain and simple. I've seen some of my colleagues use bower
and npm
interchangeably in their projects.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Found this useful explanation from http://ng-learn.org/2013/11/Bower-vs-npm/
My team moved away from Bower and migrated to npm because:
For more details, see "Why my team uses npm instead of bower".
Bower and Npm are package managers for javascript.
Bower
Bower is created solely for the front-end development. It uses flat dependency tree, requiring only one version for each package, reducing the page load. It mainly aims for minimal resource load. It has less contributors and so development process is slow.
Bower has a configuration file called bower.json. In this file we can maintain the configuration for Bower like which dependencies we need and license details, description, name and so on. Bower is suitable for front-end packages like jquery, angular, react, ember, knockout, backbone and so on.
Npm
Npm is most commonly used for managing Node.js modules, but it works for the front-end too. It uses nested dependency tree as well as flat dependency tree. It is popular and has a lot of contributors. So its new version always comes up with exciting features.
Npm has a configuration file called package.json. In this file we can maintain the configuration for Npm like which dependencies we need and license details, description, name and so on. Npm provides Dependencies and DevDependencies. Dependencies will download and maintain the front-end files like Jquery, Angular and so on. DevDependencies will download and maintain development tools like Grunt, Gulp, JSHint and so on.
This obviously doesn't work that well on the front-end, because we need jQuery in our projects. We need only one copy of jQuery, but when another package requires jQuery, then it will download again one more copy of jQuery. This is one of the main drawbacks of Npm.
Key Note : The reason many projects use both is that they use Bower for front-end packages and Npm for developer tools. Multiplying package manager in your project make your workflow harder. Npm 3 coupled with browserify or webpack is the way to go now.
2017-Oct update
Bower has finally been deprecated. End of story.
Older answer
From Mattias Petter Johansson, JavaScript developer at Spotify:
(Note that Webpack and rollup are widely regarded to be better than Browserify as of Aug 2016.)
All package managers have many downsides. You just have to pick which you can live with.
History
npm started out managing node.js modules (that's why packages go into
node_modules
by default), but it works for the front-end too when combined with Browserify or WebPack.Bower is created solely for the front-end and is optimized with that in mind.
Size of repo
npm is much, much larger than bower, including general purpose JavaScript (like
country-data
for country information orsorts
for sorting functions that is usable on the front end or the back end).Bower has a much smaller amount of packages.
Handling of styles etc
Bower includes styles etc.
npm is focused on JavaScript. Styles are either downloaded seperately or required by something like
npm-sass
orsass-npm
.Dependency handling
The biggest difference is that npm does nested dependencies (but is flat by default) while Bower requires a flat dependency tree (puts the burden of dependency resolution on the user).
A nested dependency tree means that your dependencies can have their own dependencies which can have their own, and so on. This allows for two modules to require different versions of the same depndency and still work. Note since npm v3, the dependency tree will by flat by default (saving space) and only nest where needed, eg if two dependencies need their own version of Underscore.
Some projects use both is that they use Bower for front-end packages and npm for developer tools like Yeoman, Grunt, Gulp, JSHint, CoffeeScript, etc.
Resources
This answer is an addition to the answer of Sindre Sorhus. The major difference between npm and Bower is the way they treat recursive dependencies. Note that they can be used together in a single project.
On the npm FAQ: (archive.org link from 6 Sep 2015)
On Bower homepage:
In short, npm aims for stability. Bower aims for minimal resource load. If you draw out the dependency structure, you will see this:
npm:
As you can see it installs some dependencies recursively. Dependency A has three installed instances!
Bower:
Here you see that all unique dependencies are on the same level.
So, why bother using npm?
Maybe dependency B requires a different version of dependency A than dependency C. npm installs both versions of this dependency so it will work anyway, but Bower will give you a conflict because it does not like duplication (because loading the same resource on a webpage is very inefficient and costly, also it can give some serious errors). You will have to manually pick which version you want to install. This can have the effect that one of the dependencies will break, but that is something that you will need to fix anyway.
So, the common usage is Bower for the packages that you want to publish on your webpages (e.g. runtime, where you avoid duplication), and use npm for other stuff, like testing, building, optimizing, checking, etc. (e.g. development time, where duplication is of less concern).
Update for npm 3:
npm 3 still does things differently compared to Bower. It will install the dependencies globally, but only for the first version it encounters. The other versions are installed in the tree (the parent module, then node_modules).
dep A v1.0(uses root version)For more information, I suggest reading the docs of npm 3