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.
Key Features
- Word Counting: Counts the number of words in a text or string.
- Customizable: Allows customization for handling different text formats and configurations.
- Easy Integration: Simple to install and use in Node.js applications.
- Lightweight: Minimal dependencies and efficient for basic word counting tasks.
Detailed Example with Different Texts
Let’s extend the example to work with different types of texts and observe how the word-count
module handles them:
const wordCount = require('word-count');
// Example with a simple sentence
const simpleText = "This is a simple sentence.";
console.log(`Word Count for simpleText: ${wordCount(simpleText)}`); // Output: 5
// Example with punctuation
const punctuationText = "Hello, world! How's everything going?";
console.log(`Word Count for punctuationText: ${wordCount(punctuationText)}`); // Output: 5
// Example with line breaks
const multilineText = "This is the first line.\nAnd this is the second line.\nAnd the third.";
console.log(`Word Count for multilineText: ${wordCount(multilineText)}`); // Output: 13
// Example with special characters
const specialCharText = "Special characters: #, $, %, &, * should not count as words.";
console.log(`Word Count for specialCharText: ${wordCount(specialCharText)}`); // Output: 12
// Example with numbers and mixed content
const mixedContentText = "In 2024, there are 365 days. Node.js is 15 years old.";
console.log(`Word Count for mixedContentText: ${wordCount(mixedContentText)}`); // Output: 12
Installation Steps
Step 1:
Make a folder structure for the project.
mkdir myapp
Step 2:
Navigate to the project directory
cd myapp
Step 3:
- 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
Project Structure

The updated dependencies in package.json file will look like:
"dependencies": {
"word-count": "^0.2.2"
}
Step 4:
- 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");
Step 5:
- 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
Example 1: Implementation to show by using word count module.
// index.js
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}`);
Step to Run Application: Run the application using the following command from the root directory of the project
node index.js

Example 2: Implementation to show by using word count module with another example.
// index.js
const wordCount = require('word-count');
const myString =
'Hello world! This string contains five words.';
const numWords = wordCount(myString);
console.log(numWords); // 5
Step to Run Application: Run the application using the following command from the root directory of the project
node index.js
Note: Enter the different strings of different lengths, to get different results.