Sitemap
Ace D Baugh

Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer

Node.js Modules

3 min readAug 23, 2022
Press enter or click to view image in full size
Photo by Gerry Roarty on Unsplash

Imagine, if you will, you are working on a large website that has many moving parts to it. I don’t mean just pages, but sections on the pages that do specific things. For instance, an events section or a testimonials section. Even the header is considered a section of the page that is used in multiple places throughout the website. Now you want to work on just that part while someone else wants to work on another part of the page. Instead of having one large page that every little section is built into it, you break these parts up into smaller chucks and files called modules. Modules can be parts of the page or different classes or functions in JavaScript. A common use of modules is securing private data, so it isn’t easily seen by the end user.

Modules are created by writing the snippet of code and placing an “export” somewhere on the page with what you wish to export from the code (Node.js Modules. Node, n.d.). This can be one line of code, a few lines of a function, or a long page worth of code. The snippet of code is placed in its own file with the “js” extension and placed in a folder that can be seen and accessed by other files of the project. An example of this is:

Filename: car.js

Code:

const make = “Toyota”;

const model = “Camry”;

const color = “Red”;

export {make, model, color};

Get Ace D Baugh’s stories in your inbox

Join Medium for free to get updates from this writer.

Now, this file can be accessed by other parts of the project by simply importing it by either using import syntax or the require syntax. You don’t need to import the whole file, sometimes you just want one part of the file. This makes it so you don’t run all of the code that you don’t need. For example:

Filename: index.js

Code:

import {make, color} from “./car.js”;

console.log(`I like your ${color} ${make}!`)

Node.js modules are JavaScript and so they can be run in a command prompt by simply typing node and then the file name. Most of the time you will add the import or require syntax of the file you wish to run in another file like the index.js from before. The command line interface will run it normally like any JavaScript and display the output you request (Node.js Modules. node, n.d.).

Node.js modules are great for chunking code into easily digestible and editable code. Many people can work on different parts of the project at the same time without breaking the whole thing. Modules help with making projects load faster because they only load parts of the code that are needed and Passover anything it doesn’t.

References

JavaScript Modules. JavaScript modules. (n.d.). Retrieved August 23, 2022, from https://www.w3schools.com/js/js_modules.asp

Node.js Export Module. (n.d.). Retrieved August 23, 2022, from https://www.tutorialsteacher.com/nodejs/nodejs-module-exports

Node.js Modules. node. (n.d.). Retrieved August 23, 2022, from https://nodejs.org/api/module.html

--

--

Ace D Baugh
Ace D Baugh

Written by Ace D Baugh

I am a Fine Art Photographer and Fine Code Programmer. I shoot Sony Raw and am a Full Stack Web Developer