What a Node module is and why forEach matters

A Node module is a reusable block of JavaScript code that you write once and use in many different projects. When you build a module, you often need to loop through lists of data — that is where forEach comes in. forEach is a JavaScript method that runs the same action on every item in a list, one at a time. Using forEach inside a module makes your code cleaner and easier to read than writing the same loop ten different ways.

The reason this matters: modules are how professional developers avoid rewriting code. If you build a module that processes customer data using forEach, you can drop that same module into your next project and it works when ready. You do not have to remember how you solved the problem last time.

Key Takeaways

  • A Node module is a JavaScript file that exports a function or object so other files can use it, and forEach is a method that runs code on each item in a list.
  • You create a module by writing a function, using forEach to loop through data inside it, and then exporting that function with module.exports at the bottom of the file.
  • forEach takes a callback function as its argument — that callback runs once for each item, receiving the item itself, its position in the list, and the whole list.
  • You use your module in another file by requiring it at the top, then calling the function you exported just like any other function.
  • forEach is simpler than a traditional for loop when you just need to do something to every item, but it cannot break early or skip items the way a for loop can.

The basic structure of a Node module file

Start by creating a new JavaScript file. Name it something clear — for example, processUsers.js. This file will contain your module. At the top, you can write helper functions or set up any data you need. In the middle, you write the main function that uses forEach. At the very bottom, you export that function so other files can use it.

Here is the skeleton:

The first line of your module can be blank or can require other modules if you need them. Then you write your main function. Inside that function, you use forEach on a list. At the end, you write module.exports = functionName; — this tells Node that functionName is the thing other files can use.

The export line is what makes it a module. Without it, the file is just code sitting on your computer. With it, any other JavaScript file in your project can import and use that function.

How forEach works inside your module

forEach is a method you call on a list. It takes one argument: a function. That function runs once for each item in the list. The function receives three things: the current item, the index (position in the list, starting at 0), and the whole list.

Here is a real example. Say you have a list of user objects and you want to print each user's name:

In this code, forEach runs the arrow function once for each user in the users array. The first time, user is the first object in the list. The second time, user is the second object. And so on. The function body — everything between the curly braces — runs each time.

You do not have to use all three parameters. If you only care about the item itself, you can write just user. If you need the position too, you write user, index. Most of the time you only need the first one.

Building a complete module with forEach

Let us build a real module that other files will use. Create a file called filterByAge.js. This module will take a list of people and return only the ones older than a certain age.

The module receives two things: an array of people and a minimum age. It uses forEach to look at each person. If that person is old enough, it adds them to a results list. At the end, it returns the results list. Then it exports the function so other files can use it.

Notice that forEach does not return anything — it just runs code for each item. If you need to build a new list based on the old one, you create an empty list before the forEach, then add to it inside the forEach, then return it after.

Using your module in another file

Once you have exported a function from a module, you use it in another file by requiring it at the top. Create a new file called app.js in the same folder.

The first line brings in your module. The path ./filterByAge means "look for a file called filterByAge.js in the current folder." You do not type the .js — Node adds it automatically. Then you can call filterByAge just like any other function, passing it the data it needs.

When you run node app.js in your terminal, Node loads the filterByAge module, runs the code in app.js, and prints the results. The forEach loop inside filterByAge runs silently — you only see the final result.

When forEach is the right choice versus other loops

JavaScript has several ways to loop: forEach, for, while, map, and filter. forEach is best when you want to do something to every item and you do not need to return a new list. If you need to build a new list, use map instead — it is cleaner. If you need to filter a list down to only some items, use filter.

forEach has one limitation: you cannot break out of it early or skip to the next item the way you can with a for loop. If you write break inside a forEach, you get an error. If you need to stop looping partway through, use a for loop instead.

For modules, forEach is popular because it reads like English: "for each user, do this." It is clear what the code does without having to understand index numbers and loop conditions. That clarity matters when you come back to your module six months later and need to remember what it does.

Common mistakes when building modules with forEach

The most common mistake is forgetting the export line at the bottom. You write a perfect function, but then another file tries to use it and gets an error because the module never exported it. Always end your module file with module.exports = yourFunctionName;

The second mistake is trying to return from inside the forEach. forEach does not return anything — the return statement inside the callback only exits that one iteration, not the whole function. If you need to return a value, create a variable before the forEach, build it up inside the forEach, and return it after the forEach finishes.

The third mistake is passing the wrong path when requiring a module. If your module is in a subfolder called utils, you write require('./utils/filterByAge'). If it is in the parent folder, you write require('../filterByAge'). The path matters.

Frequently Asked Questions

Can I use forEach with an object instead of an array?

forEach only works on arrays. If you have an object, use Object.keys() or Object.entries() to turn it into an array first, then forEach on that. Or use a for...in loop instead, which works directly on objects.

What is the difference between forEach and map?

forEach runs code on each item but does not return anything. map runs code on each item and returns a new array with the results. Use forEach when you want side effects (like printing or saving), and map when you want to transform a list into a new list.

Do I have to use an arrow function with forEach?

No. You can use a regular function: users.forEach(function(user) { console.log(user.name); }); Arrow functions are shorter and newer, but both work. Use whichever your team prefers.

How do I pass extra data into my module function?

Add parameters to your exported function. If filterByAge needs a minimum age, write function filterByAge(users, minAge). Then when you call it, pass both: filterByAge(myUsers, 25); The forEach inside the function can use minAge because it is in scope.

Can I use forEach in a module that runs in the browser instead of Node?

Yes. The forEach method works the same way in browsers. The only difference is how you export and import. In browsers, you use export default and import statements instead of module.exports and require. The forEach code itself is identical.