Getting Started with Node.js: From Installation to Writing Your First JavaScript Code
Downloading and Installing Node.js
Step 1: Download Node.js
-
You will see two versions available for download:
- LTS (Long-Term Support): Recommended for most users as it is more stable.
- Current: Includes the latest features but may be less stable.
-
Click on the LTS version to download the installer.
Step 2: Install Node.js
-
Run the installer that you downloaded.
-
Follow the installation wizard:
- Accept the license agreement.
- Choose the installation path (default is usually fine).
- Ensure that the option "Add to PATH" is checked (this allows you to use Node.js from the command line).
-
Complete the installation by clicking Finish.
Step 3: Verify Installation
-
Open Command Prompt (CMD) or Terminal.
-
Type the following command to check the installed Node.js version:
node -v
Writing Your First JavaScript Code in Visual Studio Code (VS Code)
Step 1: Install Visual Studio Code
- Visit the Visual Studio Code website (opens in a new tab).
- Download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the prompts to install VS Code.
Step 2: Open VS Code and Create a New File
- Launch Visual Studio Code.
- Open a new file by clicking on
File > New File
or use the shortcutCtrl + N
. - Save the file with a
.js
extension, for example,app.js
, by clicking onFile > Save As
.
Step 3: Write Your First JavaScript Code
-
In the newly created
.js
file, type the following code:console.log("Hello, World!");
-
Save the file.
Step 4: Run the Code in VS Code Terminal
-
Open the integrated terminal in VS Code by clicking on
View > Terminal
or using the shortcutCtrl + `
. -
Ensure that you are in the correct directory where your
.js
file is saved. -
Run the code by typing the following command in the terminal:
node app.js
-
You should see the output
Hello, World!
printed in the terminal.
Writing and Running JavaScript Code in Command Prompt (CMD)
Step 1: Open Command Prompt
- Press
Win + R
, typecmd
, and pressEnter
to open the Command Prompt.
Step 2: Navigate to Your Project Directory
-
Use the
cd
command to change the directory to where you want to save your JavaScript file:cd path\to\your\directory
Step 3: Create a JavaScript File
-
Create a new JavaScript file using any text editor (like Notepad) or directly from CMD:
echo console.log("Hello, World!") > app.js
Step 4: Run the JavaScript Code
-
Execute the JavaScript file using Node.js by typing:
node app.js
-
The output
Hello, World!
should appear in the Command Prompt.
âś… Tips and Additions:
- To write code more effectively, install VS Code extensions like ESLint, Prettier, and JavaScript (ES6) code snippets.
- You can install Node.js LTS using a version manager like nvm (Node Version Manager) for better control over multiple Node versions.