Learn how to install Node.js with npm on Windows and Ubuntu

Learn how to install Node.js with npm on Windows and Ubuntu

This tutorial will guide you through the process of installing Node.js and npm (Node Package Manager) on Windows and Ubuntu. After installation, we'll write a simple example to verify the setup is working correctly by outputting hello! it works!.

Prerequisites

  • Basic familiarity with command-line interfaces
  • Administrative privileges on your computer

Part 1: Installing Node.js and npm on Windows

Step 1: Download the Node.js Installer

  • Visit the Node.js official website: nodejs.org
  • Choose the correct version: Download the LTS (Long Term Support) version for better stability.
  • Run the installer: After downloading, open the installer.

Step 2: Run the Node.js Installer

  • Follow the installation steps:
    • Accept the license agreement.
    • Select the destination folder.
    • Choose the default installation options.
  • Install npm: The Node.js installer includes npm, so ensure this option is selected.
  • Complete the installation: Finish the setup, and close the installer.

Step 3: Verify Installation

  • Open Command Prompt:
    • Press Win + R, type cmd, and press Enter.
  • Check Node.js version:
    node -v
  • Check npm version:
    npm -v

Part 2: Installing Node.js and npm on Ubuntu

Step 1: Update System Packages

  • Open the Terminal by pressing Ctrl + Alt + T.
  • Update your package index:
    sudo apt update

Step 2: Install Node.js

You can install Node.js from the NodeSource repository for the most up-to-date release.

  • First, install the required prerequisites:
    sudo apt install curl software-properties-common
  • Download and install Node.js 16.x repository:
    curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
  • Install Node.js:
    sudo apt install -y nodejs

Step 3: Verify Installation

  • Check Node.js version:
    node -v
  • Check npm version:
    npm -v

Part 3: Example outputting "hello! it works!"

Now that Node.js is installed, let's create a simple program to verify the setup.

Step 1: Create a New Node.js File

  • Open Command Prompt or Terminal, navigate to the desired directory.
  • Create a new file called hello.js using your preferred text editor or command line.
  • Paste this to the file:
    console.log("hello! it works!");

Step 2: Run the Node.js File

  • Execute the file with Node.js:
    node hello.js
  • You should see the output:
    hello! it works!

Conclusion

You now have Node.js installed on both Windows and Ubuntu, and you've run a simple program to output text. This environment sets the foundation for developing applications using JavaScript and various Node.js packages.

If you encounter any issues during installation, check Node's official documentation or seek help from community forums.

Happy coding!

Related articles