Installing and Using NodeJS

Introduction

NodeJS is a JavaScript runtime designed for more advanced command-line and server-side usage. It can be thought of as “JavaScript outside of a web browser.”

Downloading

Screenshot 2018-10-25 15.18.26

There are many different downloadable versions for NodsJS across most operating systems. Often, visiting the homepage will prompt with the version detected from the browser and provide the correct build.

Installing

Screenshot 2018-10-25 15.20.52

Installing NodeJS will require administrative access to install. Once started, selecting the default options (“/usr/local/bin” for Mac and “C:\Program Files\” for Windows) when prompted is sufficient. Total installation time is usually within a couple minutes.

Once installed, it is ready for use.

Testing

Screenshot 2018-10-25 15.51.45

Testing if NodeJS is installed and configured correctly is a quick process. Opening a Terminal window (Mac) or Command Prompt (Windows) and running “node -v” should provide a response of the current version.

NPM

Screenshot 2018-10-25 16.00.21

Installing NodeJS also installed an additional tool called the Node Package Manager (NPM). It has a website for more easily finding projects, but can also be found through running “npm” on the command-line of in a Terminal window.

Using NodeJS

Screenshot 2018-10-25 16.27.23

Create or use an empty folder or directory in the current user’s folder or directiry. Name this folder “nodeTesting.”

Screenshot 2018-10-25 16.29.32

Open a Command Prompt in Widows or Terminal window on Mac.

Note: Opening the Command Prompt in Windows will default to the current user’s home folder. On Mac, opening a Terminal window will default to the user’s home directory.

Navigate to the new folder or directory. On most operating systems this will be through using the cd command and the name of the directory.

Screenshot 2018-10-25 16.31.41

In this folder or directory, type “npm install tracery-grammar” (This will install the NodeJS version of Tracery.)

windows_3

Note: NPM will produce warnings because there is not an existing “package.json” file. This can be ignored for this test.

Using either a built-in plain text editor or one like Atom, create a new file in the same folder or directory. Name this file “testing.js”

Note: NodeJS does not require that JavaScript files end in the filetype “js.” However, it is common practice and usually recommend.

Copy the following code into the file.

In the Command Prompt or Terminal window, run “node testing.js”.

windows_4

Successfully running the code will produce either “There was a deer in the forest”, “There was a rabbit in the forest”, or “There was a fox in the forest.”

NodeJS and NPM are now ready for use in other projects!

Recommendations

  • For each new NodeJS project, create a new folder or directory. This should always be the first step and helps organize node packages better, as they will be downloaded and sometimes even compiled in the local directory by default.
  • While “nodeTesting” and “testing.js” work fine for examples, naming files in useful ways can better help organize projects. Many projects use the naming scheme of “index.js” for the central or main file to be run. This is a good practice.
  • Consider using an editor like Atom or Brackets to show not only the current files, but those in the same folder or directory as well.
  • Search packages on the NPM website instead of using the tool itself. While npm can be used to search, the website provides a better pictures of how many other users have downloaded the same files and if the package has been updated recently or not.