69

Sometimes it's useful to rebuild an entire site and force bower to reinstall new versions of all the packages in bower.json.

However, there doesn't seem to be any way of doing that:

Attempt #1:

$ bower uninstall
bower not-installed 0

Nope, that only works on a package-by-package basis, even though a clean 'bower install' uses bower.json.

Attempt #2:

$ bower install -f -l 0
$

Nope, despite '-f', this does absolutely nothing if the dependencies are met.

Attempt #3:

$ rm -r bower_components
$

! Ah victory! ... wait, what's this?

rm: bower_components: No such file or directory

Oh darn, there's a .bowrc in this project that sets the directory to install things to.

My current terrible solution:

Run custom script that:

- Parse .bowerrc if one exists
- Load the directory if one is specified in the json block
- If the directory currently exists...
- ...recursively delete the directory.

It works, I suppose, but it's pretty annoying to have to setup repeatedly.

Am I missing something?

Is there not just a simple bower command to delete the local installed modules?

Seems like really basic functionality I would expect bower uninstall to do.

(This isn't really a very javascript question, but I'll happily accept something that hooks into the bower module somehow to make this happen in a simple node script)

Context

Edit: If you want 'motivation' for such a task, it's this: We have a jenkins server that builds our projects and runs tests. However, periodically it fails for no obvious reason; investigating, it's almost always because jenkins is using a previous copy of the repository with just a git-pull to update to the most recent version before building and running tests; as a result, the previous bower_components directory is there, and it is full of cached copies of the various components.

Here a few example of things which are #@$@#$'d and require bower to be run again as a forced install:

1) Some idiot (>_> fitvids) deletes the previous tagged release of a project.

2) Some project has dropped off of bower / moved its github page

3) Some project (>_> jquery) has changed the way the files are laid out in a non-major version revision.

I realize that the 'correct' solution to this problem is: fix jenkins so it creates a new temporary directory for each build. ...but that's not in my control.

So, as a build step, I need to automate a way to delete the bower components and force them to all be reinstalled; either as a grunt task (part of the build) or a jenkins build step. However, remember from (3) above, that our projects use .bowerrc, so it's not as simple as simply deleting a folder.

It would be great if I could uninstall all the existing bower components as a pre-build step to make this work.

So... back to the question: Can this be done with bower?

Doug
  • 32,844
  • 38
  • 166
  • 222

8 Answers8

148

Updated Answer

If you're trying to update all of your packages, use

$ bower update

Original Answer

Go to your bower.json file and remove all of the components, or libraries, that you want to uninstall from devDependencies.

After you have removed the ones you want gone, execute -

$ bower prune
  1. start with -

    "devDependencies": {
        "angular": "~1.2.15",
        "angular-ui-router": "~0.2.10",
        "moment": "~2.5.1"
    }
    
  2. remove angular references from file -

    "devDependencies": {
        "moment": "~2.5.1"
    }
    
  3. execute

    $ bower prune
    
  4. watch your angular dependencies get uninstalled

VtoCorleone
  • 16,813
  • 5
  • 37
  • 51
  • 1
    This works, but I'm not trying to remove a dependency or set of dependencies; I want to force bower to reinstall all of the current packages. (eg. "~1.x.x" and you want to upgrade to the latest jquery in the current revision set and uninstall the previous version, for a big long list of packages) – Doug Apr 08 '14 at 04:42
  • you may also want to `bower cache clean` afterwards – Eliran Malka Apr 08 '14 at 08:16
  • I agree with @ebohlman, why not use update? Maybe I'm confused now with what you're attempting to do. Why did you try to uninstall and rm a directory if you're just trying to update the dependencies that you already have declared? – VtoCorleone Apr 08 '14 at 16:26
  • @ebohlman bower update, even with -f does not force the install of packages. Eg. rm bower_components/momentjs/*; bower update; ls bower_components/momentjs -> lang. I want to force all packages to be reinstalled; this would effectively repair broken packages / upgrade to the latest version. – Doug Apr 10 '14 at 00:50
  • The only thing I can think of is to delete the version number in the depedencies list and rerun $bower install. Example - "dependencies":{ "moment": ""} The only down side is that you don't know the version number anymore if that's important. If it is, you can run $ bower list and it will give you all of the versions so you can enter them. – VtoCorleone Apr 10 '14 at 02:56
  • Every bower modules are installed in bower_components/ directory. If (like you said) you are able to run a script, why don't you just `rm -rf bower_components` and then run `bower i` ? It's the same thing for `npm`, you don't have a global uninstall function because it is just a matter of deleting a directory. – Leiko Apr 14 '14 at 07:40
  • @leiko nope, we use .bowerrc; see https://github.com/bower/bower#custom-install-directory – Doug Apr 14 '14 at 13:43
  • Yep, so ? Don't `rm -rf` the default __bower_components/__ but the one you have defined in your `.bowerrc` – Leiko Apr 14 '14 at 13:47
  • Well, a cleaner way to do so, would simply consist in removing Bower components from your Git repository. Jenkins is a good boy, it can re-download them on each build without problem :) Plus, from my point-of-view, it is the good way to go. Adding dependencies to repositories seems like a bad habit. – Leiko Apr 14 '14 at 13:59
  • @leiko indeed, that's what I'm doing at the moment; parse .bowerrc, rm -rf the directory, as a shell script. It just seems like something bower should be able to do using bower uninstall; but doesn't. – Doug Apr 15 '14 at 03:33
11

how about

  1. edit the bower.json
  2. 'rm -Rf bower_components/*'
  3. bower install

I was trying to upgrade to polymer 0.2.4 from 0.2.3. I can't seem to find a quick way to uninstall a set of dependencies. So I just manually removed those polymer* dir under bower_components. But for some reason bower kept remembering I had 0.2.3 installed event with bower.json modified. A 'rm -Rf bower_component/*' seems to do the tricks.

user3206144
  • 606
  • 4
  • 5
  • NB that as per the question, if you use .bowerrc, this isn't a generic solution; although basically it's the right solution, you just have to parse the .bowerrc manually in a script to find out what the local 'bower_components' folder is. – Doug May 14 '14 at 00:47
6

Actually I do something a little bit tricky but it works for me:

  1. for package in $(ls your_bower_components_folder); do bower uninstall "$package"; done;
  2. bower install
Yumarx Polanco
  • 2,195
  • 1
  • 19
  • 33
5

Uninstalling Packages

To remove a package you can use the uninstall command followed by the name of the package you wish to remove.

bower uninstall

It’s possible to remove multiple packages at once by listing the package names.

bower uninstall jquery modernizr sass-bootstrap

Nuno Sarmento
  • 415
  • 5
  • 15
  • Yes, but if you read the question you'll see I'm trying to automatically uninstall the packages without naming them each individually, by hand. I think the real answer to this question is basically; you can't. – Doug Aug 21 '15 at 02:05
  • @Doug What's wrong with the " bower update" option ? – Nuno Sarmento Aug 24 '15 at 10:55
  • It wont reinstall existing packages that aren't changed by version; even if the local copy is broken. (haha you say, no one would ever break their bower package by screwing around with it without some version number change right? .... :( ....) – Doug Aug 24 '15 at 13:09
  • @Doug - The reason, as it turns out, is that Bower doesn’t support nested dependencies. If the packages you use contain subdependencies, Bower resolves them into a flat dependency list, which must satisfy all subdependency requirements. If Bower cannot find a version of the dependency that meets all the conditions, then you get a conflict and must resolve it by hand. – Nuno Sarmento Aug 24 '15 at 13:23
  • @Doug - This article may help you (https://gofore.com/ohjelmistokehitys/stop-using-bower/ ) – Nuno Sarmento Aug 24 '15 at 13:25
0

Adapting Jumar Polanco's answer to use it in Powershell, it is possible to programmatically uninstall bower components in the following way:

In the Powershell interface, navigate to the location where bower.json and the bower_components folder is located. Usually is the root app folder.

Then you can run:

foreach($package in ls bower_components){bower uninstall $package}

Depending on what the packages dependencies are, it may be required to pay extra attention to the process, as some prompts which require extra input (Y/n) to continue the process may arise (such as dependency conflicts).

Community
  • 1
  • 1
j4v1
  • 1,457
  • 1
  • 22
  • 32
0

I don't know what build tools you use, but if it includes Grunt with grunt-bowercopy, you could use the clean option. It removes the bower_components folder (or whatever you've configured it to use) after copying out the required files.

Ideally, I'd prefer something that didn't require me to re-download all the dependencies with each build, but just the ones where doing a fresh install would find a newer version.

I'm looking for a better solution to this as well, so I'll update if I find one.

The DIMM Reaper
  • 3,558
  • 3
  • 28
  • 46
0

I've been using nombom to do this (as a bonus, it also re-installs your npm packages from scratch):

https://www.npmjs.com/package/nombom

user2926055
  • 1,963
  • 11
  • 10
0

This is what ended up working for me via Windows cmd prompt:

forfiles /p .\bower_components /c "cmd /c cd .. && bower uninstall @fname"
GaTechThomas
  • 5,421
  • 5
  • 43
  • 69