get system cpu temperature in celsius in typescript

To get the system CPU temperature in Celsius using TypeScript in Node.js, you can use the systeminformation package which provides a simple interface to access hardware information. Here's an example:

index.ts
import si from 'systeminformation';

async function getCpuTemperature() {
  const data = await si.cpuTemperature();
  const temp = data.main;
  return temp;
}

getCpuTemperature().then(temp => {
  console.log(`CPU Temperature: ${temp}°C`);
}).catch(err => {
  console.error(err);
});
284 chars
14 lines

First, we import the systeminformation package. Then we define an async function called getCpuTemperature which uses the cpuTemperature() method of systeminformation to get the temperature data. We extract the main temperature value from the data and return it.

In the main code block, we call getCpuTemperature() and log the result to the console. Note that cpuTemperature() returns the temperature in Celsius by default. If you want to get the temperature in Fahrenheit, you can pass false as the second argument to the method.

gistlibby LogSnag