From the course: Learning Node.js
Unlock the full course today
Join today to access over 23,700 courses taught by industry experts.
Write your own module - Node.js Tutorial
From the course: Learning Node.js
Write your own module
- [Instructor] Now let's take a look at how modules work inside node.js. We'll dive right in and start creating our own. Let's create a new file. So in Visual Studio code, I'll hit the new file icon and we'll call this my-module.js. And now let's create another file that we'll be using our new module. I'll call it module-demo.js. The idea is to get the code inside my module to be accessible inside our module demo. The power behind that, for example, this could be a math library that we've written and we'd like to reuse it inside different files or maybe even inside different projects. This leverages the concept of code reuse. So to continue, let's go over to my module and we'll use the exports object and we can pass in any data we'd like with exports. We'll create a property called myText and we'll set that to some placeholder value. I'll set it to hello from module. (keys clicking on keyboard) Node has a simple module loading system. In Node, files and modules are in a one to one…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.