npm的哪个版本与node的哪个版本一起提供?

lfapxunr  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(333)

npm的哪个版本与node的哪个版本一起提供?我找不到这样的名单。

nfzehxib

nfzehxib1#

https://nodejs.org/dist/处有index.json,它指示每个版本的nodejs以及与之捆绑的npm版本。

index.json

index.json数组中的一个对象的摘录如下:

[
  {
    "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
  },
  ...
]

因此数组中的每个对象都有一个version(即,nodejs版本)npm(即npm版本) 键/值对。

编程获取版本

浏览器内监听

(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脚本

考虑使用以下node.js脚本从https://nodejs.org/dist/index.json端点请求数据。

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);
  }
})();

运行以下命令:

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

将打印如下内容到您的控制台:

[
  {
    "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"
  },
  ...
]

它列出了Node.js的每个版本及其相应的NPM版本。

0md85ypi

0md85ypi2#

以下是列表:
Node >> 10.6.0 npm >> 6.1.0
node >> 9.0.0 npm >> 5.5.1
https://nodejs.org/en/download/releases/

相关问题