Sunday , 22 October 2017
Home / Programming / JavaScript / Node.js / Executing JavaScript files – Node.js Tutorial For Beginners
Executing JavaScript FIles
Executing JavaScript FIles

Executing JavaScript files – Node.js Tutorial For Beginners

We won’t be using the console anymore, but we’re going to to start using the command prompt now. Don’t be afraid! If you know nothing about the command prompt, then I’ll introduce you to some of the things we’ll need to actually use it. By the end of this tutorial, you’ll learn a few things about the command prompt and learn about executing JavaScript files.

The Command Prompt

If you’re on a mac, then this is known as the terminal. The commands you’ll learn here can be used on Windows, Mac and Linux systems. Depending on your system, you’ll have to search for your command prompt. If you’re on windows, search for cmd and open up the program you find. For mac users, search for the word terminal.

You should see something similar to this. I’m on windows, but as long as you see something with a black background and white font, then you’re using the command prompt.

Command Prompt
Command Prompt

Say hello to your new best friend! The command prompt may seem intimidating, but it’s actually quite simple to use. You’ll be using the command prompt often so let’s learn a couple of things you’ll need in order to use Node.js.

Commands

You don’t need to know every command known to man, but just a few. The first command we’ll learn is how to change the current drive. By default, the command prompt will use the drive your windows installation is on. To change the drive, you simply type in the drive letter name followed by a colon. Like so.

Command Prompt
Command Prompt

Very easy right? Now, if you would like to go to a certain directory, then you type in cd followed by the directory name. CD stands for change directory. It’s important to know that you must change drivers before you can access it’s folders and files. If you’re going to save your files in the default directory, then there’s no need to change the drive. Here’s an example of me accessing a folder in the A drive.

Command Prompt
Command Prompt

If you would like to go back one directory, then type in ../ before the directory name. The last command we’ll learn is the mkdir command. This is short for make directory. Here’s an example of me using the mkdir command.

Command Prompt
Command Prompt

Mini Exercise

Create a directory anywhere you want and then go to that directory only using the command prompt. Make sure to name this directory server as we’ll be using this directory to run our node.js files. You can use any drive you’d like. I’ll be using my drive.

Executing JavaScript Files

Node.js actually isn’t picky where you save your application files. You can have your application created in your My Documents folder or have it in a folder called WalaWalabingbang. You can even have a Node.js application on different drives. However, I like to keep things relative and therefore, I’ll be storing all my files in a folder called server. Feel free to follow my naming conventions.

Using your favourite text editor, navigate to the directory where your server is created. Create a file called index.js and insert the following code.

If you’re still in the command prompt, then you can execute the command below. With your command prompt, navigate to the folder where the index.js file is. This is very important.

You’ll see the message Hello World! outputted. What’s going on here? When you installed Node.js, the installer created a custom command called node. This command can do 2 things. If you were to just type in the command node alone, then you will be taken into a JavaScript environment. This is the same environment that the Node.js Console application uses. It functions just the same.

If you were to specify a file name, then Node.js will execute the JavaScript code in that file. You’ll notice that we omitted the .js file extension. The node command will automatically assume you want to execute a JS file. You could, however, add the .js file extension and the command will work just the same. It’s that simple!

Conclusion

You can create and execute a big JavaScript file with the node command. However, we would like to create actual web servers and that’s what we’ll do in the next tutorial. We’ll be taking the first steps to creating a blog in the next tutorial.  The actual NodeJS documentation can be found here.

About Jasko Koyn

Check Also

Display Blog Posts

Displaying Blog Posts – Node.js Tutorial for Beginners

In this tutorial, we’ll be displaying blog posts to our visitors. To do this, we …

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link