How to Perform Unit Conversions in Node.js with the 'convert-units' Module ?
Last Updated :
28 Apr, 2025
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.
Similar Reads
How to write code using module.exports in Node.js ? module is a discrete program, contained in a single file in Node.js. They are tied to files with one module per file. module.exports is an object that the current module returns when it is "required" in another program or module. We are going to see a simple code like the calculator to learn how to
3 min read
How to Handle Units and Conversions in math.js? Math.js is a great JavaScript library that makes it easy to work with units and unit conversions. It helps you define units, change between different units, and do math with them without any difficulties. In this article, we'll learn how to manage units and do conversions using Math.js.Run the below
2 min read
How to Work with 'word-count' Module in Node.js ? The word-count module is a lightweight and straightforward Node.js library designed to count words in a given text or string. It provides a convenient way to analyze text content programmatically, which can be useful in various applications such as text processing, analytics, and content management.
3 min read
How to use EcmaScript Modules in Node.js ? Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
2 min read
How to Convert a Float Number to the Whole Number in JavaScript? Given a float number and the task is to convert a float number to a whole number using JavaScript. Below are various methods to convert float numbers to whole numbers in JavaScript:Table of ContentMath.floor (floating argument)Math.ceil (floating argument) Math.round (floating argument)Math.trunc (f
4 min read
How to Add Two Numbers in Console using Node.js ? Adding two numbers using Node.js is a basic task that helps in understanding how to handle user input and process data within the Node.js environment. By following the steps in this guide, you'll learn how to create a simple Node.js application that reads numbers from the console, performs an additi
2 min read