node -v
This will display the version of Node.js installed on your system. Similarly, you can check npm (Node Package Manager), which comes with Node.js, by typing:
npm -v
Step 2: Create a New Directory for Your Project
Before you create your Node.js program, it’s a good practice to create a dedicated directory for it.
mkdir my-first-node-app
cd my-first-node-app
Here, my-first-node-app
is the name of the folder we are creating.
Step 3: Initialize a New Node.js Project
You can initialize a new Node.js project using npm. This step is optional if your project is very simple but it’s good practice, especially for bigger projects.
npm init --yes
The --yes
option automatically answers 'yes' to all prompts. This will generate a package.json
file in your project directory.
Step 4: Create Your First JavaScript File
Now, let's create a simple JavaScript file that contains our first Node.js program. You can use any text editor for this task, such as Visual Studio Code, Sublime Text, Atom, etc.
Create a file named app.js
:
touch app.js
Then open app.js
in your text editor and add the following code:
// app.js // This is a simple console.log statement
console.log('Hello, World!');
Step 5: Run Your Node.js Program
To run your Node.js program, use the Node.js runtime environment in your terminal or command prompt. Make sure you are still in your project directory.
Run your program by typing:
node app.js
You should see the following output in your terminal:
Hello, World!
Additional Example: Using Variables and Basic Math Functions
Let’s build a slightly more complex program that uses variables and basic math operations.
Create a new file mathOperations.js
:
touch mathOperations.js
Open mathOperations.js
in your text editor and add the following code:
// mathOperations.js // Variables for our simple calculation
let number1 = 10;
let number2 = 5; // Basic mathematical operations
let sum = number1 + number2;
let difference = number1 - number2;
let product = number1 * number2;
let quotient = number1 / number2; // Display the results using console log
console.log(`Number 1: ${number1}`);
console.log(`Number 2: ${number2}`);
console.log(`Sum: ${sum}`);
console.log(`Difference: ${difference}`);
console.log(`Product: ${product}`);
console.log(`Quotient: ${quotient}`);
Save the file and then run the program using Node.js:
node mathOperations.js
You should see the following output in your terminal:
Number 1: 10
Number 2: 5
Sum: 15
Difference: 5
Product: 50
Quotient: 2
Step 6: Install an External Package (Optional)
To demonstrate how to install and use an external package, let's install a package called chalk
which helps us style our console output.
Install chalk
using npm:
npm install chalk
After installation completes, chalk
will be added to your node_modules
folder and will also be listed in the dependencies
section of your package.json
.
Next, modify your app.js
file to use chalk
:
// app.js // Importing the chalk module
const chalk = require('chalk'); // Using chalk to style the console output
console.log(chalk.green('Hello, World!'));
console.log(chalk.red('This is red text'));
console.log(chalk.blueBright('And this is blue bright text'));
Save the changes and run your updated program:
node app.js
You should see the Hello, World!
message in green, This is red text
in red, and And this is blue bright text
in blue bright.
Conclusion
Login to post a comment.