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
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" }, ... ]
As you can see it lists each version of nodejs and it’s respective version of npm.