npm outdated --wanted


(Punleuk Oum) #1

I want to write an automation script which checks if a package is outdated, but only within the constraints defined in the package.json
In example, we set up our package.json like so

npm init -y
npm install --save hwrld@"<0.2"

We currently get a result that looks like this

$ npm outdated
Package  Current  Wanted  Latest  Location
hwrld      0.0.3   0.0.3   2.0.0  foo
$ echo $?
1

The ‘Wanted’ field is correct–it will output the maximum version allowable for hwrld@"<0.2", however npm outdated will still have an exit code of 1.

A theoretical npm outdated --wanted would have this result when the next newest version exceeds the semver range we defined:

$ npm outdated --wanted
$ echo $?
0

or this result when the newest version is within acceptable limits

$ npm outdated --wanted
Package  Current  Wanted  Latest  Location
hwrld      0.0.3   0.1.4   2.0.0  foo
$ echo $?
1

A workaround such as:
output=$(npm outdated --parse) || grep -P "(?!:)(.*?:)\1" <<< "$output"
may work, however, such difficult to parse logic doesn’t really belong in client code besides out of necessity.