There are three types of Node modules:
-
- Core Modules (like
fs
)
- Core Modules (like
-
- Local Modules (that you create)
-
- Third Party Modules
Core modules are great for gaining quick access to commonly-needed functionality in your program, and local modules allow you the flexibility to build out whatever tools you might possibly need, but third party modules, which fall somewhere in between, are perhaps the most exciting modules of them all!
Because developers are awesome, there are hundreds of thousands of already-written modules out there, ready for use! Each of these modules are encapsulated in a Node Package and made available via Node Package Manager or NPM, for short. A node package is a folder that contains one or more modules, a package.json
file, and any meta-info needed to make the modules work correctionly and play well with your node program.
NPM is the largest open-source software registry in the world. It includes a website, a registry of Node Packages, and a command line interface that allows us to easily incorporate packages into our programs.
Check out this NPM Intro Video
The NPM CLI makes incorporating a node package into your program fairly easy, but you can refer to this playlist if you ever get lost. It includes a whole series of videos that demonstrate the fundamentals of using NPM.
The NPM CLI installed automatically on your machine when you installed Node. Verify this now by checking the version in your terminal: npm -v
Nodemon is a package that makes developing node apps easier. It restarts the application everytime you save changes to your code.
We will use Nodemon quite a bit, so instead of installing it on each node app we build, we will install it globally. This will make it accessible to all of our node apps.
In the command line, type the following code: npm i -g nodemon
Since we're installing it globally (that's where the -g
flag comes in), it doesn't matter what directory we're in.
Let's see nodemon in action! Try running your my-first-node-app
using nodemon. Simply cd
into the directory, then run nodemon
. Nodemon knows to run the file that corresponds to main:
in your package.json
.
Now open up my-first-node-app
in your text editor. Add the following code to index.js
:
let i = 0
const count = () => {
console.log(i)
i++
}
const myTimer = setInterval(count, 1000)
Make sure to save the file and check out what's happening in your terminal!
Now change the count
function to print i*2
instead of just i
and save.
You can always restart nodemon by typing rs
.
To quit nodemon (and any program running in the terminal), press CTRL+C.
Congratulations, you've just installed and used your first third party node module!
We installed nodemon globally, but most node packages will only be useful for specific projects. In this case, when you run npm i [package name]
you want to make sure you're inside the directory of the project you want to use the package in, and leave off the -g
flag.
Day.js is a modules that helps us work with dates.
- Go to your terminal
- Make sure you're in the top level of your
my-first-node-app
folder - Type the command
npm i dayjs
- When the command is finished, go back to your text editor
- Make sure there is a folder called
node_modules
in the top level of yourmy-first-node-app
folder
- Open
index.js
in your text editor - Require the day.js library at the top of
index.js
and assign it to a variable calleddayjs
- Take a look at Day.js's Docs. There are a lot of things you can do with this module!
- Let's use the day.js module to print a date! Add the following code to your
index.js
file:
console.log(dayjs().format("MMM DD YYYY"))
- What does this print? You should have seen whatever today's date was in the format: 3-letter month, numerical day plus ordinal, and 4-digit year. For example:
Tues May 25, 2021
- Next challenge: Use day.js to pretty-print your birthday. Use the fully spelled out month, day of the week, escaped text for words such as
the
andof
, and 4-digit year. For example:
Tuesday, 20th of September, year 1985
- BONUS: Use day.js's
.fromNow()
function to print just how many years ago that birthday was!
Before we get too much further, WAIT!
Take a look at the node_modules
folder that got generated when you used the npm install
command. How big is this folder? How many files are in it? What's going on?
In general, we keep track of the version of the module that we are using in a file called package.json
. This means we can just redownload the modules based on the version number on any new computer we want to put our code on - be that a fellow developer's computer or a production server. A package.json file might look something like this:
{
"name": "node-introl",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dayjs": "^1.10.4"
}
}
Notice there is a "dependencies" section which lists our 3rd party dependencies. Right now we just have one, but others we install will go here, along with their version numbers in alphabetical order.
So, because we can just re-install the appropriate packages, we actually don't need to have the node_modules
folder to be tracked by git at all! In fact, the node_modules folder can get so huge and unweildy that it's greatly preferred that you DO NOT push them up to github nor track them with version control! In fact, this can also cause serious errors during deployment!
How can we avoid this?!
We can specify directions to git about which files it should ignore by creating a file called .gitignore
. Yes, the .
at the front is necessary!
A .gitignore
file will contain a list of files and folders that git should NOT be tracking. Go ahead and make a .gitignore
file now and put node_modules
into it as the first line.
.gitignore
node_modules
Congrats - now when you add git tracking to this folder, it will not track the node_modules folder and will not push it to github!
Other common things to ignore for git are things like .env
files which contain localized settings and possibly sensitive data like secret keys, salts, or API keys. Thus leading to this pro-tip:
Life Pro Tip: .gitignore should be one of the first files you create in a project! Create it before you do a single check-in. Always. Don't be the developer who oopsed and put an API key in public.