get system cpu temperature in fahrenheit in javascript

To get the CPU temperature of the system in Fahrenheit using Node.js, we can use the system-information package which provides an easy-to-use API for retrieving system information. To install this package, run the following command:

npm install system-information
31 chars
2 lines

Once installed, we can use the systemInformation.cpuTemperature() method to get the CPU temperature in Celsius. To convert Celsius to Fahrenheit, we can use the formula (Celsius * 9/5) + 32. Here's the code:

index.tsx
const si = require('system-information');

async function getCpuTempInFahrenheit() {
  const tempData = await si.cpuTemperature();
  const celsiusTemp = tempData.main;
  const fahrenheitTemp = (celsiusTemp * 9/5) + 32;
  return fahrenheitTemp;
}

getCpuTempInFahrenheit()
  .then(temp => console.log(`CPU temperature: ${temp}\xB0F`))
  .catch(err => console.error(err));
371 chars
13 lines

In this code, we're using an async function getCpuTempInFahrenheit() to retrieve the CPU temperature using systemInformation.cpuTemperature() method. We then convert the temperature to Fahrenheit and return it. Finally, we call the async function and log the result to the console. The result would be something like CPU temperature: 90.5°F (depending on the current temperature of the CPU).

gistlibby LogSnag