Learn Javascript

NPM & Node

Introduction to NPM and Node

slides | md

About Node

  • Node.js is Javascript for the command line.
  • Use Node to create web servers, APIs, database-driven websites, standalone software, and much more.
  • NPM (Node Package Manager) to install, manage, and incorporate open source software packages (dependencies) in your projects.

Node Installation

There are several ways to install Node (which includes NPM) on your machine

  1. Use an installer via node website (fastest, but difficult to upgrade).
  2. Use a system package manager like Homebrew or Scoop (this makes it easy to upgrade)
  3. Use nvm (the most flexible method).

For my students, I recommend #2. Choose your OS and install Node below.

(2025 note: Try #3)

👉 Install Node & NPM using Homebrew (Mac)

  1. Install Homebrew (Mac package manager) using their instructions
  2. Install Node
brew install node

👉 Install Node & NPM using Scoop (Windows)

  1. Install Scoop (Windows package manager) using their instructions
  2. Install Node
scoop install nodejs

👉 Confirm your Node version

Run these to make sure that Node installed correctly:

node -v                    # check node version
npm -v                     # check npm version

👉 Open Node interactive shell

Node is just Javascript, so almost any code will run.

  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.
> 1+1
2

👉 Run a script with Node

  1. Create a new folder named: cron-demo and open it in VS Code (in Mac you can just drag the folder from the finder to the VS Code icon in the dock)
  2. Create a file in this folder named index.js and paste the below code.
let greeting = "Hello, from Node";
console.log(greeting);
  1. In the Terminal, run* the script with node index.js

*The name index.js is arbitrary. Other names like server.js and app.js are also popular for different use cases.

👉 Run a script with Nodemon

Nodemon is an NPM package that automatically restarts your node application when file changes in the directory are detected. Run the following in the Terminal:

  1. Run npm install -g nodemon to install the nodemon package globally
  2. Run nodemon index.js (instead of node)
  3. Edit index.js and save it. The nodemon process will see the change and run your code each time you save.
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Before proceding, exit from nodemon with ctl+c

👉 The package.json file

A package.json file contains information about a project, including its name, how to test it, and dependencies.

  1. Create a new folder named cron-demo and open it in Terminal
  2. Run npm init and press return at each dialog prompt. This step initializes the project and creates a package.json file that contains any data you entered.
  3. Confirm the main field is set to index.js in your package.json file. It is simply json and you can edit it by hand.
  4. Run nodemon again (without the filename) which now knows the "main entry point" of your project and starts it.
Exit from nodemon with ctl+c

👉 Use the Cron package

Cron is an NPM package that executes scripts on a schedule (e.g. each/second, once/day) or at a particular time. Use it to automate tasks like database backups, or data or image conversion.

  1. Run npm i cron --save in the Terminal.
  2. A dependency (e.g. "cron": "^2.2.0") has been added to package.json as well as a new folder node_modules with all the dependencies it needs.
  3. Add the code below to your index.js file.
  4. Run it with nodemon index.js in the Terminal.
  5. Use crontab.guru to experiment with the time.

👉 Cron sample code

// 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'
);

👉 More about packages

  • Every package has a README with documentation. Explore it on github or npmjs.com
  • The package-lock.json manages the dependencies of your project dependencies. Best to let NPM manage this file.
  • Never commit the node_modules folder to your git repository. Instead, if you need to use this code on a new computer (or server) run npm install and it will install all your dependencies, including any security updates.

Next Steps

  1. Continue learning Node Express
  2. Examine code in demos/basic-node (modules and writing files) or see slides at end of this presentation
  3. Explore tutorials tutorialspoint, nodejs.dev, guru99.com, tutorialsteacher.com
  4. Check out sample projects using Node:

References

  1. Brown Ch1 Introducing Express (1-9) and Ch2 Getting Started with Node (11-20)
  2. w3schools nodejs, intro, command line, npm

Advanced Node Installation

  • Using NVM
  • Managing NPM packages

How to install Node using NVM

Node Version Manager (NVM) make it easy to install and use different versions of node via the command line. Reference

  1. Install Homebrew
  2. Update then install NVM
brew update
brew upgrade
brew install nvm

See these instructions for NVM, Node, NPM on Windows

How to install Node (versions) using NVM

The latest node version

nvm install v16.10.0
nvm ls
nvm use 16

The latest LTS (Long Term Support) node version

nvm install --lts
nvm ls
nvm use <NUMBER>

How to ... NVM

How to update NVM

brew update
brew upgrade # to install

How to update NPM using NVM

nvm install-latest-npm

How to upgrade NPM packages

npm-upgrade is an interactive CLI utility to easily update outdated NPM dependencies

npm i -g npm-upgrade
npm-upgrade check

Presentation comments ...