What you're building and why forEach matters

A Node module is a reusable block of JavaScript code that you can load into other projects. When you build one, you often need to loop through arrays or objects — and forEach is the standard way to do that in JavaScript. This guide walks you through creating a module from scratch, then using forEach inside it to process data.

The module you'll create will be a real file on your computer that other JavaScript files can import and use. You'll write the code, test it locally, and understand how forEach fits into the module structure.

Key Takeaways

  • A Node module is a JavaScript file that exports functions or data so other files can use them.
  • forEach loops through each item in an array and runs a function on that item, one at a time.
  • You export code from a module using module.exports, and import it using require().
  • Test your module in the same folder before moving it to other projects.
  • forEach is simpler than a for loop when you just need to do something to every item in a list.

Create a new file and set up the module structure

Start by opening a text editor and creating a new file. Name it something clear — for this example, call it processUsers.js. This file will be your module.

At the bottom of the file, add this line:

module.exports = processUsers;

This tells Node that when someone imports this file, they get the function called processUsers. Nothing happens yet — you haven't written that function. You're just setting up the export at the end so you remember to do it.

Write a function that uses forEach

Now write the actual function. Put this code at the top of your file, before the module.exports line:

function processUsers(userList) {   userList.forEach(function(user) {     console.log("Processing: " + user.name);     user.processed = true;   }); }

Here's what this does: the function takes one argument called userList, which should be an array. Then forEach runs the inner function once for each user in that array. Inside the inner function, you can access the current user and do something with it — in this case, print their name and mark them as processed.

forEach automatically passes the current item to the inner function. You don't have to write a loop counter or manage the index yourself.

Test your module in the same folder

Create a second file in the same folder called test.js. This file will test whether your module works. Write this code:

const processUsers = require('./processUsers'); const people = [   { name: 'Alice' },   { name: 'Bob' },   { name: 'Charlie' } ]; processUsers(people); console.log(people);

The first line imports your module using require(). The './' means "look in the current folder" and 'processUsers' is the filename without the .js extension. Then you create an array of user objects and pass it to your function. When you run this file with node test.js, you should see each name printed, and then the full array with processed set to true on each user.

Understand what forEach actually does

forEach is a method that exists on every array in JavaScript. It takes a function as an argument and runs that function once for each item in the array. The item itself gets passed into the function automatically.

This is different from a regular for loop, where you have to write the counter and manage the index yourself. forEach handles all that behind the scenes. If your array has 100 items, forEach runs your function 100 times without you writing any loop logic.

You can also write forEach using an arrow function, which is shorter:

userList.forEach(user => {   console.log("Processing: " + user.name);   user.processed = true; });

This does exactly the same thing. The arrow function syntax is just newer and more compact.

Common mistakes to avoid

The most common mistake is forgetting the ./ when you require a file in the same folder. If you write require('processUsers') instead of require('./processUsers'), Node will look for a package in node_modules instead of your local file, and it will fail.

Another mistake is forgetting to export your function. If you write the function but don't add the module.exports line, other files won't be able to import it. The file will run, but it won't give anything back to whoever tried to use it.

A third mistake is trying to use forEach on something that isn't an array. If you pass a string or a number to forEach, it will crash. Always make sure the data you're looping through is actually an array.

Move your module to a real project

Once your module works in the test file, you can move it to other projects. Copy the processUsers.js file into another folder where you're building something. Then in that project, use require() to import it the same way you did in test.js.

If you want to share the module with others or use it across many projects, you can publish it to npm (the Node Package Manager). That's a more advanced step, but the module itself works the same way — it's still just a file with functions that forEach loops through data.

Frequently Asked Questions

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

forEach only works on arrays. If you have an object, you need to use a different method like Object.keys() or Object.entries(). You can loop through an object's keys with Object.keys(myObject).forEach(), which converts the keys to an array first.

What's the difference between forEach and a regular for loop?

A for loop gives you more control — you can break out early or skip items. forEach runs the function on every item and doesn't let you stop it. Use forEach when you want to do something to every item. Use a for loop when you might need to exit early.

Do I have to use module.exports, or are there other ways to export?

module.exports is the standard way in Node.js. There's also ES6 export syntax (export default, export const), but that requires extra setup. For most Node projects, module.exports and require() are the simplest approach.

Can I export multiple functions from one module?

Yes. Instead of exporting a single function, export an object: module.exports = { processUsers, validateUsers, deleteUsers }. Then in another file, use const { processUsers } = require('./myModule') to import just the functions you need.

What happens if the array is empty?

forEach simply doesn't run the inner function at all. If you pass an empty array, the function body never executes, and the code moves on to the next line. This is safe — it won't crash.