Search code examples
node.jsnpm

Which versions of npm came with which versions of node?


Which versions of npm came with which versions of node? I can't find such a list.


Solution

  • At https://nodejs.org/dist/ there is index.json which indicates each version of nodejs and which version of npm is bundled with it.


    index.json

    An excerpt from one of the objects in the array of index.json reads:

    [
      {
        "version": "v10.6.0",       //<--- nodejs version
        "date": "2018-07-04",
        "files": [
          ...
        ],
        "npm": "6.1.0",             //<--- npm version
        "v8": "6.7.288.46",
        "uv": "",
        "zlib": "1.2.11",
        "openssl": "1.1.0h",
        "modules": "64",
        "lts": false
      },
      ...
    ]
    

    Whereby each object in the array has a version (i.e. the nodejs version) and npm (i.e. the npm version) key/value pair.


    Programmatically obtaining the versions

    In-browser Snippet

    (async function logVersionInfo() {
      try {
        const json = await requestVersionInfo();
        const versionInfo = extractVersionInfo(json);
        createVersionTable(versionInfo);
      }
      catch ({ message }) {
        console.error(message);
      }
    })();
    
    // Promise-wrapped XHR from @SomeKittens' answer: 
    // https://stackoverflow.com/a/30008115
    function requestVersionInfo() {
      return new Promise(function(resolve, reject) {
        let xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://nodejs.org/dist/index.json');
        xhr.onload = function() {
          if (this.status >= 200 && this.status < 300) {
            resolve(xhr.response);
          } else {
            reject({
              status: this.status,
              statusText: xhr.statusText
            });
          }
        };
        xhr.onerror = function() {
          reject({
            status: this.status,
            statusText: xhr.statusText
          });
        };
        xhr.send();
      });
    }
    
    function extractVersionInfo(json) {
      return JSON.parse(json).map(({ version, npm = '-' }) => 
        ({ nodejs: version.replace(/^v/, ''), npm })
      );
    }
    
    function createVersionTable(array) {
      const table = document.createElement("table");
      table.border = 1;
    
      const headerRow = document.createElement('tr');
    
      for (const headerText of Object.keys(array[0])) {
        const headerCell = document.createElement('th');
        headerCell.textContent = headerText;
        headerRow.appendChild(headerCell);
      }
    
      table.appendChild(headerRow);
    
      for (const el of array) {
        const tr = document.createElement("tr");
    
        for (const value of Object.values(el)) {
          const td = document.createElement("td");
          td.textContent = value;
          tr.appendChild(td);
        }
    
        table.appendChild(tr);
      }
    
      document.body.appendChild(table);
    }
    td { padding: 0 0.4rem; } tr:nth-child(even) td { background-color: antiquewhite; }

    Node.js Script

    Consider utilizing the following node.js script to request the data from the https://nodejs.org/dist/index.json endpoint.

    get-versions.js

    const { get } = require('https');
    
    const ENDPOINT = 'https://nodejs.org/dist/index.json';
    
    function requestVersionInfo(url) {
      return new Promise((resolve, reject) => {
        get(url, response => {
          let data = '';
          response.on('data', chunk => data += chunk);
          response.on('end', () => resolve(data));
        }).on('error', error => reject(new Error(error)));
      });
    }
    
    function extractVersionInfo(json) {
      return JSON.parse(json).map(({ version, npm = null }) => {
        return {
          nodejs: version.replace(/^v/, ''),
          npm
        };
      });
    }
    
    (async function logVersionInfo() {
      try {
        const json = await requestVersionInfo(ENDPOINT);
        const versionInfo = extractVersionInfo(json);
        console.log(JSON.stringify(versionInfo, null, 2));
    
      } catch ({ message }) {
        console.error(message);
      }
    })();
    

    Running the following command:

    node ./path/to/get-versions.js
    

    Will print something like the following to your console:

    [
      {
        "nodejs": "14.2.0",
        "npm": "6.14.4"
      },
      {
        "nodejs": "14.1.0",
        "npm": "6.14.4"
      },
      {
        "nodejs": "14.0.0",
        "npm": "6.14.4"
      },
      {
        "nodejs": "13.14.0",
        "npm": "6.14.4"
      },
      ...
    ]
    

    It lists each version of Node.js and its respective NPM version.