in

Node.js Basics

Introduction

Covering the basics of the Node.js Javascript engine.

Entering the node console

Node.js is what allows you to execute Javascript inside your terminal. To enter the node console inside of your terminal simply type node

the chevron indicates that we are in the node console and should now be typing Javascript instead of terminal commands

Exiting the node console

ctrl+c (twice) or type .exit will take you back to the terminal

Running Functions / Executing Scripts

In Node, defining a function inside of a file does not make it globally available to the Node console. For example let us write the following function echo

function echo(str, num){
    for(i=0; i<num; i++){
    console.log(str);
    }
}

This is a simple function that takes two arguments, a string and a number. It will print the string num number of times to the console. We save this function into a file called echo.js

If we list the directory we can see that our file is visible. Then we enter the node console and ask for the definition of echo. echo is not defined.

The trick here is that the function must be executed in the file, and then we run the file. Let us add the following to our echo.js file

function echo(str, num){
    for(i=0; i<num; i++){
    console.log(str);
    }
}

echo("First Message", 10);
echo("Second Message", 3);

So now we have defined the function and executed it twice with different inputs on the arguments. To execute these functions we must tell node to run this file. To do this we return to the terminal and call node and point it at the file

ubuntu:~/environment/nodeBasics $ node echo.js

and Node has successfully run all of the functions in the file.

NPM

node_modules Folder

Before we go all crazy installing packages with npm, we need to lay a little bit of ground-work. There are two things you need to do:

  1. Create a folder that will hold all of your downloaded packages
  2. Create a package.json configuration file

The folder that holds your node packages is always titled node_modules

This is the name that npm looks for. If you don’t have a folder named node_modules in the root directory of your application you may thrown an error.

package.json File

The package.json file is the NPM configuration file for your application. It has a lot of meta data, but the most important part is that it tracks your applications dependencies. As node packages are installed on the project, they will be automatically added to the dependencies list in the package.json file.

To launch the package.json creation utility use the following command:

npm init

After you follow the utility instructions it will automatically generate the package.json file. Here is an example of the file that will be created:

{
  "name": "expressapp",
  "version": "1.0.0",
  "description": "Our first express application",
  "main": "app.js",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Nick Coughlin",
  "license": "ISC"
}

Package Installation

To install packages with NPM in the console type

npm install nameOfPackage

and then to include the package in your application use

var someVariable = require("nameOfPackage);

for example

var cat = require("cat-me");

To install a package as well as all of the packages that package is dependent on add –save to the install command

npm install nameOfPackage --save

The full list of available modifiers on npm install is available here: npm-install

Terminal Commands Reference

Express.js Basics