Skip to content

9.3 Using Node.js

New Material
Running Node (potentially on your own computer) lets you build and test a full stack application easily before pushing your files to a server.

This tutorial covers a basic install process for Node.js. You will run these in your Terminal program.

  1. Install Homebrew (Mac package manager) using their instructions
  2. Install Node using Homebrew
Terminal window
brew install node
  1. Confirm installation
Terminal window
node -v
# -> v24.7.0
npm -v
# -> 11.5.1

Test Javascript in Node’s interactive shell.

  1. Type node on the command line and press return to open the interactive shell.
  2. At the > prompt, type 1+1 and press return. Node will evaluate your expression and return the result.
Terminal window
> 1+1
2
  1. Node is just Javascript, so almost any code will run.
  2. Cancel any process in the Terminal using Ctl+c

Inside an empty Codespaces project…

  1. Create a file named index.js and paste the below code.
let greeting = "Hello, from Node";
console.log(greeting);
  1. In the Terminal, run the script with:
Terminal window
node index.js

Every Node.js project has a package.json to store project dependencies and other metadata.

Inside an empty Codespaces project…

  1. Open the Terminal in your editor.
  2. Run npm init to initialize a new project. Press return at each prompt. This creates a package.json file with data you entered.
  3. Open your new package.json file. Confirm the main field is set to index.js.
  4. Add a new sub-property under scripts. Add a comma after the test property, then add "start": "node index.js" below it (see outcome below).
  5. Run npm run start which runs the command stored in start.
Outcome
{
"name": "cron-demo",
"version": "1.0.0",
"description": "",
"license": "ISC",
"author": "",
"type": "commonjs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
}
}

Nodemon is an NPM package that automatically restarts your node application when it detects file changes in your the project directory.

  1. Install the nodemon package globally npm install -g nodemon (the -g flag makes it global)
  2. Run index.js using nodemon (instead of node) nodemon index.js
  3. Edit and save index.js. Nodemon will restart your script each time you save.
Terminal window
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
  1. Exit the process with Ctl+C (this exits any process on the Terminal).

Cron is an NPM package that executes scripts on a schedule (e.g. each/second, once/day) or at particular times. Use it to automate any common task like database backups, etc..

  1. Run npm i cron --save in the Terminal (i is a shortcut for install, --save adds a dependency to your project’s package.json)
  2. In package.json a new dependency has been added, as well as a new node_modules folder, where the cron package and packages it uses was just installed.
  3. Add the code below to your index.js file.
// require the package
var CronJob = require('cron').CronJob;
// create a new instance
var job = new CronJob(
'* * * * * *',
function() {
console.log('The local time is: '+ new Date().toLocaleString());
},
null, true, 'America/New_York'
);
  1. Run it with nodemon index.js in the Terminal.
  2. Use crontab.guru to experiment with the time.