Node Modules
Creating, Exporting, and Importing Modules
Node's module system allows code written in one file to be exported, and then imported into other files. By importing a module (i.e. a specified section of code), we can then use that code as if it actually were written in the file we imported it to.
Let's try an example!
1. Create your module.
Inside the my-first-node-project
folder, create a javascript file called myModule.js
.
In Node, module.exports
is an object that will hold the code to be exported. We can use dot-notation to add the code we want to export to this object.
Add the following code to your myModule.js
file:
Now, our module.exports object has a key-value pair where the key is beBasic
and the value is a function.
2. Import your module in index.js
.
index.js
.This is where the require
function, specific to Node, comes into play. This function takes one argument: the path to the file that contains the module you are exporting.
In the index.js
file, write the following code:
Run index.js
via the command line:
node index.js
Voila! You've successfully created and imported a module!
Let's add some more code to our module. In myModule.js
, add the following code:
Now call this new count function from index.js
:
Try running this code in the command line: node index.js
What happened? Why didn't this work?
The exported module will only contain the code that is encapsulated in the module.exports
object!
How do we get our count
function to run? Make this happen.
Functions aren't the only things we can export! Try adding some other types of data to your module.
Further Reading
To view a practical example of importing and exporting modules, read this article. You'll see that we can export multiple functions by assigning module.exports
to an object. This is a pattern that we'll see frequently in Node.
Using Built-In Modules
It's great to have the flexibility to create our own modules, but Node supplies us with some simple built-in modules (aka core modules) that are ready for us to import and use!
Example: fs module
We will use the fs
core module (it stands for "file system") to read a text file.
Create a story.txt
text file inside your project directory and write a short story inside it.
Core modules just need to be imported using the require
function.
Write the following code to your entry point file:
Run index.js
to read your story in the terminal!
For more on the fs
module, see w3schools.
Try adding to your story using fs.write()
.
Exercise: HTTP core module
In this excercise, you will make a Hello World app from scratch by using the the HTTP core module to spin up an HTTP server.
Create a
hello-node
directory.Initialize Node in this directory.
Create your entry point file.
Import the
http
module into your entry file. (Hint: use therequire
function)Create an http server that listens to
port 8000
and writesHello, World!
to the client. (Hint: look up the core http module on w3schools)Run the server using the command
node index.js
.Check to see that your program is working by visiting
localhost:8000
in your browser.
SOLUTION
Last updated