Master W3Schools Node JS Modules: The Ultimate Guide

Logan Jul 13, 2026

Do you want to enhance your understanding of Node.js modules? Then, you've come to the right place. Here, we'll explore how to use, require, and export modules in Node.js, making use of W3Schools' tutorials and examples. Let's start our journey into the world of Node.js modules.

the cover of 6 node j s projects idea
the cover of 6 node j s projects idea

Before diving into the specifics, let's first understand the basics. In Node.js, a module is a file that contains JavaScript code. These files can be reused and shared across different projects, promoting code reusability and maintainability. Now that we have a solid foundation, let's dig deeper into the world of Node.js modules.

Start Building with Node.js: A Beginner-Friendly Guide
Start Building with Node.js: A Beginner-Friendly Guide

Requiring Modules in Node.js

One of the primary ways to work with modules in Node.js is by requiring them. The 'require' function allows you to import modules into your current script. Here's how you can use it:

School Management System in Node JS with Source Code
School Management System in Node JS with Source Code

First, let's create a module named 'greet.js' to greet the user:

```javascript function greet(name) { console.log(`Hello, ${name}!`); } ```

Next, we'll use another JavaScript file (let's name it 'app.js') to require and use the 'greet.js' module:

the cover of task management app
the cover of task management app

```javascript const greet = require('./greet'); greet('John Doe'); ```

Using 'require' with Different File Types

The 'require' function can also import modules from different file types. For instance, you can import JSON files using 'require':

Create a JSON file (named 'data.json') that contains user data:

Node js
Node js

```json { "name": "John Doe", "age": 30 } ```

Then, in your JavaScript file (let's say 'app.js'), you can require and use this JSON data:

```javascript const data = require('./data.json'); console.log(data.name); // Outputs: John Doe ```

Requiring Core Modules

Node.js also allows you to require its built-in or core modules. For example, the 'http' module lets you create a simple HTTP server:

Node.js Project Folder Structure
Node.js Project Folder Structure

```javascript const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }); server.listen(8080, '127.0.0.1'); console.log('Server running at http://127.0.0.1:8080/'); ```

Now that we've explored requiring modules in Node.js let's delve into exporting modules.

Exporting Modules in Node.js

the backend folder structure is shown in this screenshote, which shows an array of
the backend folder structure is shown in this screenshote, which shows an array of
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
Node.js & Express.js: Build Fast & Scalable Web Apps 🚀🌍
Add Dark Mode to Your Next.js + Tailwind CSS Website in 60 Seconds
Add Dark Mode to Your Next.js + Tailwind CSS Website in 60 Seconds
Best Node.js Project Ideas
Best Node.js Project Ideas
Node.js x Python: Qual a Melhor Tecnologia Para o Back-end
Node.js x Python: Qual a Melhor Tecnologia Para o Back-end
HTTP method express
HTTP method express
Express js vs Node js
Express js vs Node js
Next.js
Next.js
Node.js Libraries That Will Simplify Your Backend Workflow
Node.js Libraries That Will Simplify Your Backend Workflow
18 Best Free Node.js Tutorials To Improve Your Coding Skills 2023 - Colorlib
18 Best Free Node.js Tutorials To Improve Your Coding Skills 2023 - Colorlib
3d Website with Three.js + CDN example
3d Website with Three.js + CDN example
Node.JS 22 - Latest Features and Updates
Node.JS 22 - Latest Features and Updates
an image of a computer screen with some type of text on the bottom right corner
an image of a computer screen with some type of text on the bottom right corner
JavaScript Ecosystem Explained: React, Next.js, Angular, Vue, Node.js, Express and Modern
JavaScript Ecosystem Explained: React, Next.js, Angular, Vue, Node.js, Express and Modern
JavaScript vs Node JS
JavaScript vs Node JS
a poster with some writing on it and an image of the words in different languages
a poster with some writing on it and an image of the words in different languages
Learn Node.js and Express with This Free 8-hour Back End Development Course
Learn Node.js and Express with This Free 8-hour Back End Development Course
the different types of web pages that can be used to create an app or website
the different types of web pages that can be used to create an app or website
Key Features of Node js
Key Features of Node js
10+ Top Node.js Frameworks for Building Scalable Web Applications
10+ Top Node.js Frameworks for Building Scalable Web Applications

Exporting is the other side of the coin when dealing with modules in Node.js. It allows you to share your code as reusable chunks. There are two ways to export modules in Node.js: using modules.exports and export statements.

Using Module.exports

The 'module.exports' object is what you can alter to determine what gets exported from a module. Here's an example where we export a function and an object:

```javascript function greet(name) { return `Hello, ${name}!`; } module.exports = { greet, getName: () => 'John Doe', }; ```

Then, in your 'app.js' file, you can import and use these exports:

```javascript const { greet, getName } = require('./greet'); console.log(greet(getName())); // Outputs: Hello, John Doe! ```

Using Export Statements

Since Node.js version 13.2.0, you can also use export statements to export modules. Here's an updated version of our 'greet.js' module using the new syntax:

```javascript function greet(name) { return `Hello, ${name}!`; } export { greet, default: getName }; ```

And in your 'app.js' file:

```javascript import { greet, default as getName } from './greet.js'; console.log(greet(getName())); // Outputs: Hello, John Doe! ```

Mastering Node.js modules is an essential step in becoming a proficient Node.js developer. W3Schools offers numerous tutorials and examples to help you understand and implement modules in your projects. So, go ahead and start experimenting with modules today!