Get Started with Node.js Development
Welcome to this hands-on guide where you’ll learn how to get started with Node.js development. Whether you’re on macOS or Windows, this guide will walk you through the installation process, running your first Node.js code, importing an NPM package, and using it in your JavaScript code. We’ll be using Visual Studio Code as our code editor, which provides a seamless development experience for Node.js projects.
Prerequisites
Before you begin, ensure you have the following prerequisites:
- A computer running either macOS or Windows.
- Internet access to download necessary software.
- Basic familiarity with your operating system’s command line interface.
Step 1: Install Node.js
Node.js is a JavaScript runtime that allows you to run JavaScript code on the server-side. You can download an install Node.js from the official Node.js website at https://nodeje.org
Step 2: Install Visual Studio Code
While you can use any code editor, one of the most popular code editors is Visual Studio Code (VSCode). You can download and install VSCode from https://code.visualstudio.com
Step 3: Create a Node.js Project
Now that you have Node.js and Visual Studio Code installed, let’s create a simple Node.js project:
- Open Visual Studio Code.
- Click on “File” > “Open Folder” and create a new folder for your project.
- Inside your project folder, create a new file named
app.js
. - In
app.js
, add code below. -
Save the file.
// app.js console.log("Hello, Node.js!");
This code will simply write a “Hello Node.js!” message to the console when run.
Step 4: Run Your Node.js Code
Let’s run the Node.js code you just created:
- Open a terminal within Visual Studio Code by pressing Ctrl + or clicking on “Terminal” > “New Terminal”.
- Navigate to your project folder using the terminal.
-
To execute your
app.js
code, run the following command in the terminal:node app.js
- You should see the output
Hello, Node.js!
printed in the terminal, indicating that your Node.js code executed successfully.
Step 5: Importing a NPM Package
Now, let’s import the ‘chalk’ NPM package and use it in your Node.js code:
- Open a terminal within Visual Studio Code if it’s not already open.
- Navigate to your project folder using the terminal.
-
Run the following command to initialize a new package.json file:
npm init -y
This command initializes a new package.json file with default values.
-
Now, let’s install the
chalk
NPM package, which is a library for stylingconsole.log
messages with colors.Run the following command:
npm install chalk
- Once
chalk
is installed, open app.js in Visual Studio Code. -
Update the code in
app.js
to importchalk
and use it:// app.js const chalk = require('chalk'); // Example usage of chalk to style console output console.log(chalk.blue('Hello, Node.js!')); console.log(chalk.green('This text is green!')); console.log(chalk.red.bold('Error: Something went wrong!'));
- Save the file.
Step 6: Run Your Updated Node.js Code
Let’s run the updated Node.js code with the imported chalk
package:
- Make sure you’re still in the project folder in the terminal.
-
Run the following command to execute your updated app.js file:
node app.js
- You should see the output displaying styled text using
chalk
, indicating thatchalk
was successfully imported and used in your Node.js code.
Congratulations! Now wasn’t that easy? You’ve successfully created, executed, imported an NPM package (chalk
), and used it in your first Node.js project using Visual Studio Code. You’re now equipped to explore more advanced Node.js development techniques and build powerful applications.