129

In my project I've installed bower components without save option. Now, I would like update to bower.json?

How can I update bower.json with installed packages?

Ashraf.Shk786
  • 570
  • 1
  • 9
  • 23
kongaraju
  • 8,507
  • 9
  • 48
  • 75
  • 2
    The lack of a one step way to do this seems like quite a deficiency. Since there's an uninstall command, it seems like running `bower install` without `--save` would always be harder and never easier. – Chris Jul 04 '14 at 19:25
  • 1
    If you have nothing in your bower.json, just remove it and type `bower init` in your console. – Jean-Luc Barat Apr 06 '17 at 09:49

6 Answers6

146

Just list your dependencies:

bower list

Then you should run all install command with param '--save' like this:

bower install bootstrap --save

It's a hard work, but if you have a thousand dependencies, could you create a script to automatize the task.

Helder Robalo
  • 1,793
  • 2
  • 12
  • 15
  • This is a great answer. Thanks! – jessegavin Feb 07 '14 at 17:26
  • 1
    I found this got problematic with just ~10 packages, as it's not obvious which packages were downloaded as dependencies of others, which caused bower to prompt for version numbers. Even with the correct answers, the resulting bower.json file was not quite right. I worked with Sebastien's answer, which worked for me. – gb2d Nov 02 '15 at 11:58
  • The dependencies that not has declared in bower.json are identified by "extraneous" tag on console. – otaviodecampos Jan 18 '18 at 13:52
135

A little trick if you don't want to write a script for that:

before doing anything rename your bower.json in bower2.json for example.

then you can do a:

$ bower init

(automatically create a bower.json file).

note that all questions should be pre-filled with your current config.

When it will ask you:

set currently installed components as dependencies? say yes,

You now have all your dependencies in the new bower.json file (and you can check if everything is right with the old bower2.json)

Sebastien Horin
  • 8,814
  • 4
  • 39
  • 51
  • What is the registry option it mentions when doing this - I had assumed this command would be doing just a local thing. I elected to keep mine private with that handy question but would like to learn what that meant - is there some kind of internet bower registry. I am just inline thinking this is how bower might do an npm install kind of thing. – landed May 11 '15 at 08:16
  • 2
    @landed : that would be the bower packages registry (http://bower.io/search/) HTH – FOR Dec 15 '15 at 13:31
  • This didn't work out for me because `bower.json` did not list `angular:'1.5.5'`. This is because I had `angular-animate` installed and since it depends on `angular 1.5.5` bower decided not to include `angular` in the `bower.json` (considering it will install it anyways when someone does a `bower install` for this `bower.json`). This will be an issue if someone remove `angular-animate` from `bower.json` in which case `bower` will not install `angular` too since its not listed. – Aakash Apr 25 '16 at 06:32
35

A bit arduous way is to run bower list, look for packages labeled extraneous and add those manually to the dependencies in the bower.json.

If there are a lot of extraneous packages, it might be easier to workaround this by running bower init and answering Yes to "set currently installed components as dependencies?". This will take your current bower.json, read it and then create new one using information from the old one. So in an ideal case you will have the same file just with extraneous packages added.

Warning: Firstly, there might be something lost in the process (e.g. devDependecies). Secondly in the last version of bower (v1.2.7) this will not preserve current packages info! I feel it is a bug. However you can save the old file and (manually) merge it with the generated one.

Also, you should ask for an option or something by opening a bower issue as this would be welcomed by many developers.

Robin Pokorny
  • 9,951
  • 1
  • 21
  • 28
28

You can use bower-check-updates (you need installed node.js on your machine):

bower-check-updates is a utility that automatically adjusts a bower.json with the latest version of all dependencies

bower-check-updates - is a fork of npm-check-updates, so it's all the same but updates bower.json, instead of package.json

npm install -g bower-check-updates
bower-check-updates -u
bower install 

This will install bower-check-updates globally, so you can launch it from anywhere.

P.S. for more information about npm-check-updates pleas see this topic

Community
  • 1
  • 1
Sergei Panfilov
  • 12,838
  • 13
  • 66
  • 89
2

If there aren't that many bower packages you have installed, try writing bower install [package_name] --save. This will just update your bower.json file.

superjisan
  • 710
  • 7
  • 11
0

After bower-check-updates -u you must run bower install instead of npm install

Arend
  • 3,668
  • 2
  • 24
  • 37
Ámon Tamás
  • 121
  • 1
  • 8