Node.js is a popular runtime environment that allows you to run JavaScript code on the server-side. Here's a step-by-step guide to installing Node.js on a typical Windows operating system and a simple example of running a JavaScript file using Node.js. Remember that Node.js is not limited to running just simple scripts like the one below; it's commonly used for building web applications, APIs, servers, and more.
Step 1: Download Node.js
- Open your web browser and navigate to the official Node.js website: https://nodejs.org/
- On the homepage, you will see two download options: LTS (Long Term Support) and Current. It's generally recommended to download the LTS version for stability.
- Click on the "LTS" button to download the installer for the Long Term Support version.
Step 2: Install Node.js
- Run the downloaded installer (the file will have a .msi extension).
- The installer will guide you through the installation process. You can choose the default settings or customize them based on your preferences.
- Once the installation is complete, Node.js and its package manager, npm (Node Package Manager), will be installed on your system.
Step 3: Verify Installation
- Open the Command Prompt or your preferred terminal application.
- To verify that Node.js and npm are installed correctly, type the following commands and press Enter after each:
node -v npm -v
These commands will display the installed version of Node.js and npm, respectively.
Step 4: Create and Run a Simple Example
- Create a new file named hello.js in a directory of your choice.
- Open hello.js in a text editor and add the following code:
console.log("Hello, Node.js!");
3. Save the file.
Step 5: Run the Example
- Open your Command Prompt or terminal.
- Navigate to the directory where you saved hello.js using the cd command. For example, if you saved it on your desktop
- cd Desktop
- Run the JavaScript file using Node.js:
- node hello.js
- You should see the output in the terminal:
- Hello Node.js !