Node.js: Code Outside the Browser / Introduction to Node.js

Installing Node.js & npm

✏️ Install Node.js

  1. Go to: https://nodejs.org

  2. Click on the LTS version (Recommended for most users).

  3. Run the installer:

    • Keep everything default ➜ Click Next → Install.

  4. After installing

  5. open the terminal

  6. type the code below in the terminbal

node -v     # Should show the version (e.g. v20.19.0)
npm -v      # Should show npm version

Now Node.js & npm are installed.

  1. type node and press enter. This starts an interactive shell


🧰 Node Version Manager (NVM)

⬇️ NVM Installation Guide

🔹 What is NVM?

  • NVM stands for Node Version Manager.

  • It is a command-line tool that lets you install, manage, and switch between different versions of Node.js on your machine.

  • Super helpful if you're working on multiple projects that each require a different Node version.

🛠️ Why use NVM?

  • Avoid compatibility issues with projects needing different Node versions.

  • Quickly test how code behaves on older or newer versions of Node.

  • Easily switch versions with a single command.

💻 Common NVM Commands

Command

Purpose

nvm install <version>

Installs a specific version of Node.js (e.g., nvm install 18)

nvm use <version>

Switches to the specified version

nvm list or nvm ls

Shows all installed versions

nvm install node

Installs the latest version

nvm uninstall <version>

Removes a specific version

nvm alias default <version>

Sets a default version to use in new terminals