With npm, when I use the npm view [package]
command, I can see the publish date relative to today, for example:
$ npm view lodash
lodash@4.17.21 | MIT | deps: none | versions: 114
[...]
published over a year ago by bnjmnt4n <benjamin@dev.ofcr.se>
How can I get the exact publish date-time in a format such as YYYY-MM-DD HH:mm:ss
?
tl;dr - Utilize the solution provided in the "Solution" section below.
Using npm view
you can run the following command:
$ npm view express time --json
This logs something like the following to the console:
{ "modified": "2018-10-31T23:01:06.660Z", "created": "2010-12-29T19:38:25.450Z", "0.14.0": "2010-12-29T19:38:25.450Z", "0.14.1": "2010-12-29T19:38:25.450Z", ... "4.16.3": "2018-03-12T17:50:14.119Z", "4.16.4": "2018-10-11T03:59:14.308Z", "5.0.0-alpha.7": "2018-10-27T03:12:11.060Z" }
As you can see, the command (above) returns a JSON object containing properties; modified
, created
, and also has properties for each version (e.g. "0.14.0"
, "0.14.1"
, etc... ). The associated value for each property is a date.
The docs for npm-view
state the following;
You can view child fields by separating them with a period.
Therefore, obtaining the values (i.e. dates) for modified
and created
you can run either of the following commands respectively:
$ npm view express time.modified
# prints --> `2018-10-31T23:01:06.660Z`
and
$ npm view express time.created
# prints --> `2010-12-29T19:38:25.450Z`
However, when obtaining the value/date for a specific version property/key such as 4.16.4
you'll need to use a different approach, because commands such as the following do not work:
# This does not work...
$ npm view express time.4.16.4
# This also does not work...
$ npm view express time.'4.16.4'
# This does not work either...
$ npm view express time["4.16.4"]
Solution:
The following command demonstrates how to successfully obtain the published date for version 4.16.4
of the express
package:
$ npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['4.16.4'])});"
# prints: --> 2018-10-11T03:59:14.308Z
Note: You'll need to replace the '4.16.4'
part with the appropriate version as required.
This solution:
npm view express time --json
command and pipes the JSON to a nodejs script.process.stdin
to read the piped JSON from stdin
(fd 0).JSON.parse
to parse the JSON string, and obtain the value of the property/key named 4.16.4
Note
If you want the published date for the latest version you could run the following two bash commands:
$ version=$(npm view express version)
$ npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});"
# prints: --> 2018-10-11T03:59:14.308Z
Here we firstly run npm view express version
(to obtain the latest version number) and assign the value returned to the variable named version
(i.e. we utilize Command Substitution). Then we reference the version
value in the node script.
You can also chain the two commands using the &&
operator to form a one line command as follows:
$ version=$(npm view express version) && npm view express time --json | node -e "process.stdin.on('data', function(data) {console.log(JSON.parse(data)['"$version"'])});"
# prints: --> 2018-10-11T03:59:14.308Z