From the course: Node.js Essential Training

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Using the require function

Using the require function - Node.js Tutorial

From the course: Node.js Essential Training

Using the require function

- So what else do we have globally available to us in Node.js? Well, we have some references to the current file and the current directory. So let's replace this with a few console.log messages. Remember, console.log is available to us globally without having to import anything. So we want to log the __dirname and the __filename. We also want to make sure that we're in the right folder. So we will make sure that we're navigated correctly, typing cd, dragging that file directory over. And then we're going to say node global. So now this is going to point directly at the directory where the file is located and then directly at the file. So notice that the filename variable includes the entire path. So also available to us globally is the common.js module pattern. So this is the pattern we're going to use to import other code into our files. One way that we can import these other modules is we can use this function.…

Contents