02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Loading Packages and Using Modules in Node.js

Loading Packages and Using Modules in Node.js

In this tutorial, we will explore how to load packages and use modules in Node.js. Modules are an essential part of Node.js, allowing you to organize your code, reuse functionality, and separate concerns within your applications. Understanding how to load modules effectively is key to building well-structured and maintainable Node.js projects. This tutorial was originally created in 2016, and includes updates to make it more relevant today.

Step 1: What are Modules?

In Node.js, modules are reusable pieces of code that can be loaded into your application. They help in splitting a program into smaller, manageable files. Node.js has a few types of modules:

  • Built-in Modules: These are the core modules that come with Node.js (e.g., http, fs, path).
  • Third-Party Modules: These are modules created by the Node.js community that can be installed using NPM.
  • Custom Modules: These are modules that you create yourself to encapsulate specific functionality.

Step 2: Loading Built-in Modules

Node.js comes with several built-in modules that you can load into your project using the require function. Let’s start by loading a built-in module.

Example: Loading the http Module

The http module allows you to create a simple server. Here is how you can load it:

const http = require('http');

The require() function is used to load modules, and http is the name of the built-in module that we are loading here.

Step 3: Installing and Loading Third-Party Modules

You can install third-party modules from NPM (Node Package Manager) and then load them into your application. For example, let's install the popular lodash package.

Installing lodash

To install lodash, use the following command in your terminal:

npm install lodash

Loading lodash

After installing lodash, you can load it into your project by requiring it:

const _ = require('lodash');

Now you can use lodash’s utility functions in your project to work with arrays, objects, and other types of data more efficiently.

Critical Update (2024): Newer versions of Node.js support ES Modules natively. You can use import instead of require if you use an .mjs file extension or set "type": "module" in your package.json.

Step 4: Creating Custom Modules

You can also create your own modules to encapsulate code and reuse it across different parts of your application. Let's create a simple custom module.

Example: Creating a Custom Module

  1. Create a file named greet.js:

    // greet.js
    module.exports = function(name) {
      return `Hello, ${name}!`;
    };
    
  2. Use the Custom Module in Another File:

    // app.js
    const greet = require('./greet');
    
    console.log(greet('World'));  // Output: Hello, World!
    

In this example, we created a custom module named greet.js that exports a function. We then used require() in app.js to load this custom module and call the function.

Step 5: Organizing Your Application with Modules

Using modules effectively can make your Node.js application more organized and easier to maintain. Each module can focus on a specific part of your application, such as handling routing, database interaction, or utility functions.

  • Separation of Concerns: By dividing your code into different modules, you can keep each module focused on a specific task. This makes your code more readable and maintainable.
  • Reusability: Modules allow you to reuse code across different parts of your application, reducing duplication and keeping your code DRY (Don’t Repeat Yourself).

Conclusion

In this tutorial, we covered how to load packages and use modules in Node.js. You learned about the different types of modules (built-in, third-party, and custom), how to load them, and how to create your own modules. Modules are a fundamental part of Node.js that help keep your code organized, reusable, and maintainable.

By understanding how to effectively use modules, you can build more scalable and efficient Node.js applications. In future tutorials, we'll cover more advanced topics, such as handling module dependencies, working with ES modules, and creating more complex server applications using Node.js.

Video Transcript

00:01Now that we've been working with node js
00:03and NPM for a bit, I want to differentiate between packages
00:07and modules where a package we've met
00:11and worked with throughout our NPM training.
00:14As we said, packages are loaded through NPM.
00:18Once we're working inside of node js, we're not working
00:22with packages, but we're kind of looking within a package
00:25and taking out of it a module.
00:27And as such, node js requires modules.
00:30A package could have one module
00:32or it could have many, many modules.
00:35Traditionally speaking, less is more with NPM
00:39because you want small, very powerful packages
00:41that are shareable instead of a lot
00:43of packages sitting within one package.
00:46But let's look into it.
00:47Let's work with these packages and modules
00:50and start using them in a real world
00:52programming type of way.
00:56Part of the core philosophy of node js
00:58and NPM is that packages that you'll depend on,
01:03packages and modules.
01:05Now I want to talk specifically
01:07what is a package versus a module.
01:09If you're gonna read online
01:10or get other books, you're gonna see those
01:12terminologies often.
01:14Now, when we're talking about packages,
01:16we've seen packages previously, and we've seen packages
01:20because every single thing we install is a package.
01:23So when we installed is array, it is a package.
01:26So let, let me just install it again
01:27and I'm gonna ask for version one with all minor updates,
01:32including patches.
01:33Now, if you don't know what that means, please go back
01:36to our NPM chapter
01:37and I'm gonna make sure that I save it into our package.
01:41Js ON file. If I go ahead
01:44and install is array,
01:46what I'm actually doing is I'm installing a package.
01:49A package could contain multiple modules
01:52and a module, it could be a package.
01:54There's only one module, just like the case of is array.
01:57So if I go back into my folder
01:59and I in my folder itself, you'll see
02:01that in my modules I have here my IS array module.
02:05And if I look into the actual file itself,
02:07all it really contains is one index file.
02:09Just like our application has an index file
02:13and in it, an operation is being, is being done.
02:16In this case, we're basically testing to see
02:19or adding into the, uh, adding into the array itself,
02:24a function or a module expert into the arrays itself.
02:27Test for it to able to be able to test
02:30to see if it is an array.
02:32Now, that's literally all this file does,
02:34and this file has only one module.
02:37That specific package has only one module.
02:40And if we take a deeper look into it, we're gonna see
02:43that our, our module itself is, uh, exported.
02:47And we're not gonna get into the syntax of this,
02:49of this logic flow
02:50because all we really want to do is know
02:52how to work with it.
02:53So I want to go back into my application now
02:56that I have it installed.
02:58I want to go into my index and start using it.
03:01Now, the way I use it,
03:02and you'll always find a read me file in these applications
03:05as well, and you'll see in the Read Me
03:08file, how do you use it?
03:09In this case, we're requesting the is array
03:12and assigning the value
03:13that comes back into the is array variable.
03:18Now our Isra variable is actually a function,
03:20and we could then use that Isra to test
03:23and see if different elements are an array or not.
03:27We've already installed it
03:28so we don't have to do that again.
03:30But the require process,
03:32although it seems um, like we're loading in now a package,
03:36we've loaded a package,
03:37but we're asking for the specific functionality,
03:40the specific pack, the specific module of is array.
03:44Now, in general, when when you decide
03:46that you're gonna create your own package,
03:48you wanna really create one thing
03:50that your package does well,
03:51that your module in the package does well, really your,
03:54the idea is to create small packages
03:57that only do very the the, the only thing that they're meant
04:01to do and one thing, and they do it really well.
04:04So let's take a look at it.
04:05Let's take a look at it in action
04:06and literally just from taking that documentation,
04:09I'm just gonna go ahead here
04:10and I'm gonna create that variable is array
04:14and I'm gonna assign to the require.
04:17And what am I gonna require? I'm gonna require is array.
04:21So what I'm doing here, and I by the way, I don't have
04:23to specify do, uh, js, all I have to do is name the
04:27module that I want to use.
04:29Alright? So I install the package
04:31and I'm using a module in that package.
04:33In this case, the package has only one module,
04:35but there's many packages
04:37that have multiple different modules,
04:39which you would load each module separately
04:41with the new require command.
04:44And as we saw in the documentation of this module
04:47of this package, we could see
04:49that we have here the way we use it, we call that function
04:52and send into it an array.
04:54Now, if you wanna take a deeper look
04:55and understand how this logic works, you'll see that it has,
04:59here, it exports a function,
05:01which basically checks the see does the, does the array,
05:05does the built in array have an is array?
05:08If it does, it will return. That is array.
05:11And if it doesn't, it will create a new function
05:13that accepts an array and will return it
05:16and will return, uh, the basic call to test
05:19to see if it's an array.
05:20So this is how you would actually create a module.
05:23We're now going into the logic in, in node js of how
05:26to create, but you're more than encouraged
05:28to download different packages and just open them up
05:31and see how they work, especially these more simple ones.
05:34So you could test them out and see how they work.
05:37Now, beyond that, one thing that I would want to suggest is
05:41if you wanna become more an expert of no jazz, make sure
05:45to grab a course that is really dedicated, purely a no jazz,
05:49because there's so much wonderful things that you could
05:51and should and wanna know and no jazz.
05:53But the intention of this title is to get you comfortable
05:56with the world of full stack development,
05:59not necessarily become an expert in any specific
06:02chapter that we've covered.
06:04So if any one of the chapters are something you want
06:06to DD dive deeper into that, I encourage you to do so.
06:09So let's go ahead and o to, and I'm gonna go ahead
06:11and create here another variable,
06:13and I'm just gonna call it a for an array.
06:15And it was gonna send here, um, a value and another value
06:19and lemme create here something that's not a array.
06:21So it's gonna be an S for a string
06:24and here it's gonna be, um,
06:28I'm not an array.
06:30Alright, so let's go ahead and also test this out.
06:32So I'm gonna still use my console log
06:35and I'm gonna ask is,
06:42is a, is a n uh, ray,
06:50And I'm gonna go ahead here
06:52and just send with my is array function
06:56and I'm gonna send in that array
06:58and lemme go ahead and note.
06:59So if already we're doing this,
07:00let's do another console call.
07:02And in the next console call we're gonna ask is SNRA.
07:06And we're gonna get that answer
07:07by calling our Is Array function.
07:09Now obviously this module is a little bit of a silly module.
07:13If JavaScript already has a built-in is Array
07:16and we're working on the server, so we're making sure
07:18that we have the latest version anyways.
07:22So if I go ahead here and I just run my application,
07:24once I saved it, I'm gonna go into terminal
07:26and just like when I, uh, originally when I called the node
07:30and I set the file name,
07:32this is exactly what I'm gonna do right now.
07:33So lemme just clear everything
07:34so we'll have a clear view when we're doing it
07:37and you'll see that is a and a it sure is, is SNRA.
07:40No, it is not an array.
07:42So that is the basics of working
07:44with node packages and modules.
07:46And again, the difference between a package
07:49and a module is a module lives within a package.
07:52You don't really approach packages directly in no js.
07:56You use NPM to load up a package
07:59and that package could contain one module or a few modules.
08:02It's important to understand this difference
08:04because when you're reading up documentations online,
08:07you might get confused between the module and a package,
08:10and it's important to know the difference between the two.
08:13In this lecture, we really sealed the deal about the
08:16differences between packages and modules,
08:18and now you know how to require different modules
08:21and how to load different packages using NP
08:25in the next lecture, we're gonna take it a step further
08:27as we configure for the first time a running server.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!