Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See exact publish date of an npm package

Tags:

npm

With npm 6.x, when I use the npm view [package] command, I can see the publish date relative to today, for example:

$ npm view express

[email protected] | MIT | deps: 30 | versions: 261

[... other info about express package ]

published 3 weeks ago by dougwilson <[email protected]>

How can I get the exact publish date-time in a format such as YYYY-MM-DD HH:mm:ss?

like image 407
Zongque Xu Avatar asked Nov 05 '18 05:11

Zongque Xu


People also ask

How do I check my npm packages?

Use the npm list to show the installed packages in the current project as a dependency tree. Use npm list --depth=n to show the dependency tree with a specified depth. Use npm list --prod to show packages in the dependencies . Use npm list --dev to show packages in the devDependencies .

Does npm pack publish?

Description. Publishes a package to the registry so that it can be installed by name. By default npm will publish to the public registry.

What npm publish does?

When you run npm publish , npm bundles up all the files in the current directory. It makes a few decisions for you about what to include and what to ignore. To make these decisions, it uses the contents of several files in your project directory. These files include .


1 Answers

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.


How to obtain the published date for a specific version?

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:

  1. Runs the npm view express time --json command and pipes the JSON to a nodejs script.
  2. The nodejs script utilizes the builtin process.stdin to read the piped JSON from stdin (fd 0).
  3. We then utilize 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
like image 104
RobC Avatar answered Sep 23 '22 21:09

RobC