Open In App

How to Work with ‘word-count’ Module in Node.js ?

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

The ‘work-count‘ module is a simple and lightweight external NPM module that can count the number of words in a string. This module provides a count() function, which counts the number of words by ignoring any special characters or symbols in the string. 

Installation: First, we have to install the module using the command given below. Open the terminal, navigate to the same folder as your project, and then give the same command to install the module. After installation, a package.json file gets added to your project folder, which contains all the information regarding the installed modules.

$ npm install word-count  

 Installation

Use the module: To use this module in the application, we have to incorporate the module into the project using the require keyword. Give the string to the module to process it and then show the output in the console of the terminal.

const wordCount = require("word-count");

 

Run: After writing all the code, open the terminal and use the given command to run the JavaScript file. This will count the words in the entered string and show the result in the console. Give different strings again to again, which will produce different output every time. 

$ node index.js

where index.js is the javascript file name.

Example 1:

Javascript




const wordCount = require("word-count");
  
// String to count
const str = 
"GeeksforGeeks is the best website for computer science";
  
const count = wordCount(str);
  
console.log(`Number of words: ${count}`);


Output:

output 1

Example 2:

Javascript




const wordCount = require('word-count');
  
const myString = 
'Hello world! This string contains five words.';
  
const numWords = wordCount(myString);
  
console.log(numWords); // 5


Output:

output 2 

Note: Enter the different strings of different lengths, to get different results.



Similar Reads

What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ?
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line. Difference between the "word-break: break-all;" and "word-wrap: break-word;" word-break: break-all; It is used to break the words at any ch
2 min read
How to export promises from one module to another module node.js ?
JavaScript is an asynchronous single-threaded programming language. Asynchronous means multiple processes are handled at the same time. Callback functions vary for the asynchronous nature of the JavaScript language. A callback is a function that is called when a task is completed, thus helps in preventing any kind of blocking and a callback functio
1 min read
How to work Mongojs module in Node.js ?
Mongojs module is a built-in module in node.js that facilitates the usage of MongoDB with Node.js. It offers almost all the functionality that is provided by the official API of MongoDB and is used extensively by developers across the globe. Follow the steps mentioned below to install mongojs module and get it up and running. Project Setup and Modu
3 min read
What are the differences between HTTP module and Express.js module ?
HTTP and Express both are used in NodeJS for development. In this article, we'll go through HTTP and express modules separately HTTP: It is an in-build module which is pre-installed along with NodeJS. It is used to create server and set up connections. Using this connection, data sending and receiving can be done as long as connections use a hypert
2 min read
How to allow classes defined in a module that can be accessible outside of the module ?
The TypeScript scripts written by default are in the global scope which means that all the functions, methods, variables, etc. in one file are accessible in all other TypeScript files. This can lead to conflicts in variables, functions as programmers can edit the function/variable name or value without any realization. Therefore, the concept of mod
3 min read
How to allow classes defined in a module to be accessible outside of a module ?
In typescript by using an import statement, a module can be utilized in another module. any variables, classes, methods, or other objects declared in a module are not accessible outside of it. The keyword "export" may be used to construct a module, and the term "import" can be used to import it into another module. Let's demonstrate how to do this.
3 min read
How do you import a module into another module Angular?
Angular applications are modular by design, allowing you to break down complex functionalities into smaller, manageable pieces called modules. One common task in Angular development is importing modules into other modules to make their components, directives, pipes, and services available throughout the application. This article explores the proces
4 min read
Pluralize and Singularize any Word using the 'pluralize' NPM Module
The 'pluralize' npm module is a very important and useful NPM module that can convert singular English words to their plural form and vice versa also. This module uses a pre-defined list of rules, applied in order, to singularize or pluralize a given word which can be used in node.js applications or projects. Using the 'pluralize' module in Node.js
3 min read
How does the cluster module work ?
In this article, we will learn what is cluster modules and how does it works in node.js. Let's get started. Node.js is a single-threaded machine that uses javascript as its scripting language. Due to a single thread in node.js, it handles memory more efficiently because there are no multiple threads due to which no thread management is needed. Now
3 min read
Javascript Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks
3 min read