Skip to content

Latest commit

Β 

History

History
198 lines (126 loc) Β· 8.13 KB

NodeModules.md

File metadata and controls

198 lines (126 loc) Β· 8.13 KB

πŸ“¦ Building and Deploying a Node Module

⏱ Agenda {docsify-ignore}

  1. [05m] πŸ† Learning Outcomes
  2. [20m] β˜€οΈ Warmup: Coding Required
  3. [30m] πŸ’¬ TT: NPM
    1. What is NPM?
    2. What is a Package?
    3. What Happens When I Install a Package?
    4. package.json
    5. What was the "^" for the version numbers for?
    6. How Do I Use a Package?
    7. Useful Commands
    8. Deploying Your Modules
  4. [15m] 🌴 BREAK
  5. [15m] πŸ‘€ Demo: Tocsify
  6. [60m] πŸ’» Activity: Create a Node Module

[05m] πŸ† Learning Outcomes

By the end of this lesson, you should be able to...

  1. Explain how Node Modules work.
  2. Make your own Node Module.
  3. Deploy your Node Module on NPM

[20m] β˜€οΈ Warmup: Coding Required

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.

[30m] πŸ’¬ TT: NPM

What is NPM?

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.

What is a Package?

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!

What Happens When I Install a Package?

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.

package.json

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/app
  • version: 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"
    }
}

What's With the ^ Symbols in Versions?

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!

How Do I Use a Package?

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 use require().

Example

// Load the full build.
var lodash = require('lodash');

Now you can call anything in the lodash library by using the lodash variable!

Useful Commands

  • which npm – where npm was installed
  • npm --version – which version of npm you have
  • npm install <package_name> – install a package, or run without the package name to install everything in the package.json file
  • npm 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 installed
  • npm init – creates a package.json file for you! Packages installed will be automatically added
  • npm update <package_name> – updates a package (or all packages if none are specified)
  • npm uninstall <package_name> – uninstalls a package
  • npm help – if you need help with npm!

Deploying Your Modules

Why Should I Deploy a Module?

  • 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.

How Do I Deploy?

Follow this quick guide to deploy your first module!

The hardest part will be coming up with a unique name!

[15m] 🌴 BREAK {docsify-ignore}

[15m] πŸ‘€ Demo: Tocsify

Let's go through an example of an NPM module built by Dani: Tocsify

[60m] πŸ’» Activity: Create a Node Module

Now it's your turn to create more node modules! Take some time to create modules that do the following:

  1. Write a random number generator module
  2. Write a module that removes all spaces from a string and replaces them with dashes (-)
  3. Write a module of your choosing!

Stretch challenges: Pick an idea from the following lists, and create a module for it!

πŸ“š Resources & Credits

  1. What is npm
  2. Installing NPM packages locally
  3. Creating Node.js Modules (npm doc) (video & documentation)
  4. Publishing npm Packages (npm doc)
  5. How to Create and Publish your First Node.js Module (a beautiful article!)
  6. Building Your First Node Module
  7. What is this Javascript β€œrequire”?
  8. What is require? | Node.js