How to Install Node.js and npm on Windows and Mac

Category: Software Install and Setup

Node.js is a popular runtime environment for JavaScript, and npm is its package manager. If you're a beginner, this guide will help you install and set up Node.js and npm on Windows and Mac.

Step 1: Download Node.js

  • Visit the official Node.js download page.
  • Download the latest LTS (Long-Term Support) version for your operating system.

Step 2: Install Node.js on Windows

  1. Run the downloaded installer.
  2. Follow the setup wizard and keep the default options.
  3. Ensure the option "Automatically install npm" is selected.
  4. Click "Install" and wait for the process to finish.

Step 3: Install Node.js on Mac

On macOS, you can install Node.js using Homebrew:

brew install node

Or, you can install it using the official installer from Node.js website.

Step 4: Verify Installation

To check if Node.js and npm are installed correctly, open a terminal (Command Prompt, PowerShell, or Terminal on Mac) and run:

node -v

Output should show the installed Node.js version.

npm -v

Output should display the npm version.

Step 5: Configure npm (Optional)

Set a default global directory for npm packages:

npm config set prefix "C:\\Users\\YourUsername\\npm-global"

Add the directory to your system's PATH variable to access npm commands globally.

Step 6: Install a Test Package

To test npm, install a package like express:

npm install -g express

Check installed packages:

npm list -g --depth=0

Step 7: Create a Simple Node.js App

Create a new JavaScript file app.js and add:

console.log("Hello, Node.js!");

Run the script using:

node app.js

Step 8: Update Node.js and npm

To update npm, run:

npm install -g npm

To update Node.js, download the latest version from the Node.js website or use:

nvm install node

Conclusion

Congratulations! You have successfully installed Node.js and npm on Windows and Mac. Now you can start building JavaScript applications. For more details, check the official Node.js documentation.