Section 2: Installing and Exploring Node.js

Part 4. Installing Node.js and Visual Studio Code

Visit nodejs.org > Downloads. Download the Current version and install.

Test node.js by opening WebStorm and typing the following in the WebStorm terminal to see the node version that is installed.

$ node -v
Updating node.js

We can update Node.js to the latest version using Node.js’ version manager named n. We can use npm to install n, as shown below.

$ npm install -g n

Once n is installed, we can use n to update Node.js to the latest version.

$ n latest

5. What is Node.js?

Per the Node.js website, “Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine.”

Chrome uses the V8 engine (written in C++) to run JavaScript code that has been dowloaded from a web server. Node.js is able to extend the capabilities of JavaScript by adding to V8 C++ binding to system calls. This allows Node.js’ V8 to do things that would be unsafe in a browser, such as access the computer’s file system.

You can run JavaScript code from the browser’s console and from the node REPL (read-eval-print loop). Try.

  • The browser has access to the window object and document object.
  • Node does not have access to the window object or the document object.
  • Node has access to an object named global and an object named process.
  • The browser does not have access to the global and process objects.

Verify this by trying to access each of these objects in both the browser’s console and in the node REPL.

$ window
$ document
$ global
$ process.exit()

Part 6. Why Should I Use Node.js?

Per the node.js website, “Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.” Event-driven refers to the ability to register callbacks that will be executed when an I/O operation has completed. Non-blocking refers to the fact that Node.js does not have to wait for an I/O operation to complete before executing the next line of JavaScript.

npm is a package management service that was installed on your machine when Node.js was installed. We can use it to install pre-written packages on our machine and use them in our apps. You can browse all of the libraries and packages that npm has available by visiting npmjs.com.

Part 7. Your First Node.js Script

In WebStorm create a directory on your web server for your node code. Create a file within that directory named hello.js. Add to hello.js the following code.

console.log('hello')

Now run hello.js from WebStorm’s console using node.js. From the console, change the working directory to the directory containing hello.js. Next, execute hello.js using node by typing the following command.

$ node hello.js

You should see the word hello printed to the console.

Node.js’ website contains documentation for the Node.js API. Take some time to browse the documentation and see what is available.

© 2020 – 2024, Eric McGregor. All rights reserved. Unauthorized use, distribution and/or duplication of this material without express and written permission from the author is strictly prohibited.

Leave a Reply