Update all npm packages to the latest version

Fun with bash.  This is a command that will update all of your packages that are behind, to the latest version.  I know you can do npm update but this doesn’t seem to do it.  For some reason.  Whatever, this was fun.

npm outdated | awk 'NR!=1&&$4!="git" {printf "%s%s%s\n", $1,"@", $4}' | xargs -t -n1 npm update --save

npm outdated will give a result like this

Package Current Wanted Latest Location
co-body 4.0.0 1.2.0 4.0.0 co-body
co-views 2.1.0 0.3.0 2.1.0 co-views
corelogger 0.0.1 git git corelogger
dagon 0.2.1 git git dagon
del 2.0.2 1.2.1 2.0.2 del
eventstore 0.0.1 git git eventstore
koa 1.0.0 0.21.0 1.0.0 koa
koa-generic-session 1.9.2 1.8.0 1.9.2 koa-generic-session
koa-logger 1.3.0 1.2.2 1.3.0 koa-logger
koa-passport 1.2.0 1.1.6 1.2.0 koa-passport
koa-router 5.0.1 5.0.1 5.2.3 koa-router
mocha 2.3.3 2.1.0 2.3.3 mocha
must 0.13.1 0.13.0-beta2 0.13.1 must
path 0.11.14 0.11.14 0.12.7 path
readstorerepository 0.0.1 git git readstorerepository
rx 2.5.3 2.5.3 4.0.0 rx

Christ.  That’s a five column table that looks a lot better in your terminal.

awk 'NR!=1&&$4!="git"

This line says NR!=1 ignore the first line which is just the headers. AND remove lines where the fourth column is “git”.  This will be the case if you have dependencies that are in git and not in npm

{printf "%s%s%s\n", $1,"@", $4}'

This says print column 1 then “@” then column 4 then a new line.  If you don’t do it with the printf which is a print format I presume you end up with co-body @ 4.0.0 rather that co-body@4.0.0 which npm wont like.  So now you will have

co-body@4.0.0
co-views@2.1.0
del@2.0.2
koa@1.0.0
koa-generic-session@1.9.2
koa-logger@1.3.0
koa-passport@1.2.0
koa-router@5.2.3
mocha@2.3.3
must@0.13.1

Pipe this into

xargs -t -n1 npm update --save

Which says, for each line do npm update arg and print the line out so we can see what’s going on. xargs -n1 will print each line of the input only once per command ( the npm update ).  the -t is verbose ( shows the line before running it ) .  I guess you could do it with out the -n1 actually, but whatever.  –save will of course update your package.json.

Now if anyone reads this they may comment “oh here’s how you do it with npm in 5 characters” and I’ll feel dumb, but this was fun anyway.

Advertisements
Update all npm packages to the latest version

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s