Open In App

How to Perform Unit Conversions in Node.js with the ‘convert-units’ Module ?

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The ‘convert-units‘ is a very useful and important NPM module that can be used for converting various units of measurement. This module supports a wide range of units including length, mass, volume, temperature, pressure, and more. While working with data, we encounter data of different types and this module plays an important role in these situations to the conversion of data from one unit to another.

How to Perform Unit Conversions in Node.js with the ‘convert-units’ Module?

Installation: To use this package in our application, we have to install it first. Follow the steps given below to follow the module ‘convert-units’.

  • Open the terminal in your PC and navigate to the same directory of the project using the commands.
  • Install the module using the command given below.
  • After we install the module, a JSON file add to the directory which contains all the details of the installed modules.
$ npm install convert-units

 

Import the module: After installation, we have to import the module into our project or application. Follow the given steps to import the module.

  • require function is used to import the installed module.
  • Pass the installed module name to require function.
  • convert() function is very important in this module. Pass the value to this function that you want to convert from one unit to another.
const convert = require('convert-units');

Run the code:

  • After importing the module and writing the code of our application, we can run the file using the following command. 
  • This command run the file using node followed by file name. Here, in this command index.js is the file name.
  • This will show the output of the code file.
$ node index.js

Example 1: Converting value from kilograms to pounds

In this example, we first import the ‘convert-units’ module and then use the convert() function to convert the value 5 from kilograms to pounds. We specify the source unit with the .from() method and the target unit with the .to() method. The result of the conversion is stored in the result variable and then printed to the console.

Javascript




// Import the module
const convert = require('convert-units');
  
const result = convert(5).from('kg').to('lb');
  
console.log(result); // Output


Output: 

11.0231

Example 2: Converting Celsius to Fahrenheit

Javascript




// Import the module
const convert = require('convert-units');
  
const result = convert(25).from('C').to('F');
  
console.log(result); // Output


Output: 

77

Conclusion: The ‘convert-units‘ NPM module is a powerful module for performing unit conversions in Node.js. It provides a simple and easy-to-use API for converting values between various units of measurement, such as length, mass, volume, temperature, and more. With just a few lines of code, developers can use this module to perform accurate and reliable unit conversions in their projects. Whether you need to convert kilograms to pounds, Celsius to Fahrenheit, or any other unit of measurement, the ‘convert-units’ module is an excellent choice for handling your conversion needs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads