After v6.5.0 running npm/lib/view twice for the same package name throws an error

I have some code that utilises npm/lib/view, all works fine with up to and including v6.5.0, after that an error is thrown. I have extracted a sample of the code to demonstrate the problem.

const npm = require('npm');

npm.load({}, function callback(err, result) {
  if (err) {
    throw err;
  } else {
    const name = 'json3';
    const view = require('npm/lib/view');

    view([name, 'dist-tags'], true, function cb(error, desc) {
      if (error) {
        throw error;
      } else {
        console.log(desc);
      }
    });

    view([name, 'dist-tags'], true, function cb(error, desc) {
      if (error) {
        throw error;
      } else {
        console.log(desc);
      }
    });
  }
});

When executing the above with v5.10.0 up to and including v6.5.0 I get the following result on node v8, v10 and v12

$ node test-view.js 
{ '3.3.3': { 'dist-tags': { latest: '3.3.3' } } }
{ '3.3.3': { 'dist-tags': { latest: '3.3.3' } } }

After that, example with latest v6.11.3, on node v8, v10 and v12

$ node test-view.js 
{ '3.3.3': { 'dist-tags': { latest: '3.3.3' } } }
/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/async.js:61
        fn = function () { throw arg; };
                           ^

TypeError: Invalid Version: 0
    at new SemVer (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/semver/semver.js:323:11)
    at compare (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/semver/semver.js:614:10)
    at compareLoose (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/semver/semver.js:619:10)
    at Array.sort (native)
    at packument.catch.then.data (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/lib/view.js:161:43)
    at tryCatcher (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/promise.js:699:18)
    at _drainQueueStep (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues (/Users/grahamfairweather/.private/project-updater-x/tmp/salita/node_modules/npm/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)

So, I am wondering if I am doing something wrong or perhaps there is a bug in npm? There was a major refactor in npm after v6.5.0

It seems that a change is required like this

const npm = require('npm');

npm.load({}, function callback(err, result) {
  if (err) {
    throw err;
  } else {
    const name = 'json3';
    const view = require('npm/lib/view');

    view([name, 'dist-tags'], true, function cb(error, desc) {
      if (error) {
        throw error;
      } else {
        console.log(desc);
        view([name, 'dist-tags'], true, function cb1(error1, desc1) {
          if (error1) {
            throw error1;
          } else {
            console.log(desc1);
          }
        });
      }
    });
  }
});

And then things appear to be a success.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.