I will go ahead and save this, and we can go back out to the terminal to run it to see what happens. So, from the terminal, this time, I'm going to run node global.js, and this time when I run it, I see an undefined. That is because node js works a little bit different than the browser when it comes to storing variables. Let's go back to our code. Every node js file that we create is it's own module. Any variable that we create in a node js file, is scoped only to that module. That means that our variables are not added to the global object the way that they are in the browser.
We will slice out the first 17 characters of this string. That should give us, just node. Since node js four and above, now support some ES six, I can use this justNode variable in a template string. Instead of logging hello down here on line five, I'm going to log the justNode variable in a template string. So, to do that, I'm going to use to two backtick characters. These are not single quotes. These are the backtick character. This is usually found under the escape key in the upper left hand corner of your keyboard.
I can write a string in between these backticks. If I want to include the value of a variable, all I need to use is a dollar sign and a couple of curly brackets. In between the curly brackets, we can place the variable that we want to use. So, justNode. We will go ahead and add this substring here using a template string. Let's go ahead and save this, and navigate back to the terminal and run our file node global.js. We can see Rock on World from justNode js that was cut out of our initial string.
If we go back to our code, there are many other things that are available to us globally in our code. One of the things that's nice to use is a reference to the current directory that we're in and a reference to the current node module that we're using. If I were to log __dirname. We would get the full path to the current directory where this module is located. If I were to log__file name, we can get the full path to the current file as well as this file's name. So, I'm going to ahead and save this, and navigate back to the terminal, and let's run our file again, node global.js, and we can see our first console log Rock On World from Node js.
The second console log, is the full path to the directory that this file is located in. The third console log, is the full path to the directory, that this file is located in with the file name. Now, if I wanted to just pluck the global.js file name from that second string, I can use some tools that are available to us, with our install. Let's go back to the code. Another thing that's available to us globally, is the require function. The require function, is what we're going to use to import other node js modules.
So, here at line one, I'll go ahead and drop my cursor and hit enter a few times. Now, I'm going to create a variable for the path. I will load this path instance using require. So, I'm going to require the path module. The path module, is a module that is available to you with your installation of node js. It gives us some tools for working with paths. Now, let's delete these last console logs down here. I'm also going to delete the Hello, and justNode. This time, what we're going to do is, in this template string Rock on World from justNode, we're going to use the path module .base name function to pluck the base file name from a full path.
I'll put in __filename, and that should pluck just the global.js file name from that full path. Let's go ahead and save this. Navigate back to the terminal, and run our file one more time. Now, this time, when I run the file, I'm going to leave off that js extension. When we run a node js file, we can include that js extension, or not. It's assumed that the file is going to be js, so when we node global, node js will find the file that's marked global.js and run that.
So, here we can see Rock On World from global.js. So, the node js global object contains those objects and functions that are available to us globally. Meaning, that we can start adding these objects to our node js code immediately. Now, in the next couple lessons, we're going to continue to work with this global object, and we will discover the process object, as well, as the timing functions. Then, we'll take a deep dive into the common js module pattern. Which includes module, exports, and require.
Alex Banks shows how to install Node.js on a Mac or PC and work with the Node.js core: the global object, event loop, http module, and file system. Then he covers reading and writing data, streaming data, making http requests, and working with the node package manager (npm). In the final chapters, he shows how to start sessions on web servers, communicate with web servers and clients with WebSockets, use the Express framework to develop applications, and test and debug Node.js code. Want to speed up your Node.js workflow? Check out the tutorials on automating tasks with Grunt and npm scripts.
- What is Node.js?
- Installing Node.js
- Understanding the global object and global timing
- Importing the core modules
- Handling events
- Creating child processes
- Reading, writing, and removing files
- Working with file streams
- Making http requests
- Serving files and JSON data
- Installing npm, the node package manager
- Working with Express, the web server framework
- Using WebSockets
- Testing and debugging Node.js code
- Working with Grunt and Browserify
- Automating tasks with npm scripts