- [05m] π Learning Outcomes
- [20m] βοΈ Warmup: Coding Required
- [30m] π¬ TT: NPM
- [15m] π΄ BREAK
- [15m] π Demo: Tocsify
- [60m] π» Activity: Create a Node Module
By the end of this lesson, you should be able to...
- Explain how Node Modules work.
- Make your own Node Module.
- Deploy your Node Module on NPM
Today, we'll be learning about the inner workings of Node modules. To better understand how modules work, let's dig in to a function we see at the top of every Node file: require
!
Node was the first environment to offer a way to organize code in modules by using a special function called
require()
. How does it work? Letβs try to implement it from zero.
Follow Michele Nasti's guide "Let's write our simple version of the require() function" in your breakout room.
NPM is a company that promotes open source development for all things JavaScript! What they're most known for though, is the Node Package Manager (also NPM) which allows for the easy installation of JavaScript modules.
Think of it like a library you're installing to help your project do things! NPM is like Brew but for your JS project!
From time manipulation to web frameworks, to creating user interfaces, NPM allows your project to take advantage of all sorts of libraries to solve your problems!
You install packages with NPM by running npm install <package_name>
. If the folder doesn't already exist, a node_modules
folder will be added to your project, and all modules installed by NPM will live there.
What you may find though is that a package may have dependencies that it needs to run. So sometimes installing one package means installing many more! Don't worry, NPM will install any dependencies needed for you.
What if your buddy wants to clone or fork your project? He'll have to manually install all those modules. That's a drag.
An easy way to fix this is by having a package.json
file, which lists all modules that a project needs in order to run. Think of it like a dependency list!
A package.json
file has two required fields:
name
: name of your project/appversion
: version number
It will then have a list of dependencies
, or modules that need to be installed so that the project/app can run. Here's an example:
{
"name": "secret-app",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.11",
"moment": "^2.29.1"
}
}
That means we want the stated version or higher for our project!
Now that we have a package.json
, all anyone has to do is run npm install
, and all dependencies listed in package.json
will be installed!
By using the require
function at the top of any server-side JavaScript file. The require()
function, module.exports
and exports
are APIs of the module system that is specific to Node.js. Browsers do not implement this module system.
require()
is not part of the standard JavaScript API.- Built-in function with only one purpose --- to load modules.
- Each module has its own scope.
- To expose values from a module, assign those values to
module.exports
. - To access another module's exported values (
module.exports
), it must userequire()
.
// Load the full build.
var lodash = require('lodash');
Now you can call anything in the lodash library by using the lodash
variable!
which npm
β where npm was installednpm --version
β which version of npm you havenpm install <package_name>
β install a package, or run without the package name to install everything in thepackage.json
filenpm install -g <package_name>
β installs the package as a global package (available from the command line)npm install <package_name> --save-dev
β install a package that is only needed for development. For example, it can be any package for testing the project.npm list
β list of npm packages installednpm init
β creates apackage.json
file for you! Packages installed will be automatically addednpm update <package_name>
β updates a package (or all packages if none are specified)npm uninstall <package_name>
β uninstalls a packagenpm help
β if you need help with npm!
- Help out other programmers that may need to solve a similar problem
- Contribute to Open Source and participate in the community
- Make it easy to install a Node program in any new environment without cloning and running the code first.
Follow this quick guide to deploy your first module!
The hardest part will be coming up with a unique name!
Let's go through an example of an NPM module built by Dani: Tocsify
Now it's your turn to create more node modules! Take some time to create modules that do the following:
- Write a random number generator module
- Write a module that removes all spaces from a string and replaces them with dashes (-)
- Write a module of your choosing!
Stretch challenges: Pick an idea from the following lists, and create a module for it!
- What is npm
- Installing NPM packages locally
- Creating Node.js Modules (npm doc) (video & documentation)
- Publishing npm Packages (npm doc)
- How to Create and Publish your First Node.js Module (a beautiful article!)
- Building Your First Node Module
- What is this Javascript βrequireβ?
- What is require? | Node.js