Node.js Installation and Setup Practice Questions with Solutions

Introduction

Before writing Node.js applications, you need to install Node.js correctly and prepare your development environment. In this chapter, you will practice the basic installation and setup steps, including checking Node.js and npm versions, creating your first project folder, running a JavaScript file, creating package.json, and installing a package. These examples are beginner-friendly and help you build a strong foundation for working with Node.js projects. Node.js Installation and Setup practice questions with solutions help to understand the concepts.

Question 1: How do you check whether Node.js is installed?

Problem

Use the terminal to check whether Node.js is installed on your computer.

Solution

Open Command Prompt, PowerShell, or Terminal and run:

node --version

You can also use:

node -v

Output

You should see a version similar to:

v22.x.x

The exact version will depend on the Node.js version installed on your computer.

Step-by-Step Explanation

  1. Open your terminal.
  2. Type node --version.
  3. Press Enter.
  4. If Node.js is installed, its version will be displayed.
  5. If the command is not recognized, Node.js may not be installed or may not be correctly added to your system PATH.

Question 2: How do you check the npm version?

Problem

Check whether npm is installed and find its current version.

Solution

Run:

npm --version

You can also use:

npm -v

Output

Example:

10.x.x

Step-by-Step Explanation

  1. Open your terminal.
  2. Type npm --version.
  3. Press Enter.
  4. npm displays its installed version.
  5. npm is normally installed along with Node.js.

npm is important because you will use it to install packages and manage Node.js projects.


Question 3: How do you check both Node.js and npm versions together?

Problem

Check the installed versions of both Node.js and npm.

Solution

Run these commands:

node -v
npm -v

Output

Example:

v22.x.x
10.x.x

Step-by-Step Explanation

  1. node -v displays the Node.js version.
  2. npm -v displays the npm version.
  3. Both commands help you confirm that your Node.js development environment is working.
  4. The exact version numbers may be different on your computer.

Question 4: How do you create a Node.js project folder?

Problem

Create a new folder named my-node-app and move into it using the terminal.

Solution

On Windows, you can run:

mkdir my-node-app
cd my-node-app

Output

There may be no special output after these commands.

Your terminal will now be working inside the my-node-app folder.

Step-by-Step Explanation

  1. mkdir my-node-app creates a new folder.
  2. cd my-node-app moves into that folder.
  3. You can now create your Node.js project files inside this folder.

A separate folder for each project keeps your files organized.


Question 5: How do you create your first Node.js JavaScript file?

Problem

Create an app.js file and use Node.js to run it.

Solution

Create a file named:

app.js

Add this code:

console.log("My first Node.js application");

Then run:

node app.js

Output

My first Node.js application

Step-by-Step Explanation

  1. Create a project folder.
  2. Create a file named app.js.
  3. Write JavaScript code inside the file.
  4. Open the terminal in the project folder.
  5. Run node app.js.
  6. Node.js executes the JavaScript code.
  7. The message appears in the terminal.

This is one of the simplest ways to confirm that your Node.js environment is ready.


Question 6: How do you create a package.json file?

Problem

Create a package.json file for a new Node.js project.

Solution

Inside your project folder, run:

npm init

npm will ask several questions.

For a quick setup, you can use:

npm init -y

Output

You should see a message similar to:

Wrote to .../my-node-app/package.json

A basic package.json may look like:

{
  "name": "my-node-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Step-by-Step Explanation

  1. Open the terminal inside your project folder.
  2. Run npm init -y.
  3. npm creates a package.json file.
  4. This file stores important information about your Node.js project.
  5. It can also store dependencies and project scripts.

The -y option accepts the default answers automatically.


Question 7: How do you create a Node.js project with a custom name?

Problem

Create a Node.js project and give it a custom project name.

Solution

Run:

npm init

When npm asks for the package name, enter:

student-node-app

You can accept the remaining default options by pressing Enter.

Output

A package.json file will be created with a project name similar to:

{
  "name": "student-node-app",
  "version": "1.0.0"
}

Step-by-Step Explanation

  1. Open your project folder.
  2. Run npm init.
  3. npm starts the project setup process.
  4. Enter your preferred package name.
  5. Provide other information or accept the defaults.
  6. npm creates the package.json file.

This method is useful when you want more control over your project’s initial configuration.


Question 8: How do you install a Node.js package?

Problem

Install the popular lodash package in your Node.js project.

Solution

First create or enter your project folder:

cd my-node-app

Then run:

npm install lodash

Output

npm will display installation information, and your project will contain a node_modules folder.

Your package.json will also contain a dependency similar to:

{
  "dependencies": {
    "lodash": "^4.17.21"
  }
}

The exact version may be different.

Step-by-Step Explanation

  1. Open your Node.js project folder.
  2. Run npm install lodash.
  3. npm downloads the package.
  4. npm creates or updates the node_modules folder.
  5. npm records the package in package.json.
  6. The package is now available for your project.

You will learn more about npm packages and dependencies in later chapters.


Question 9: How do you check whether a Node.js project is working correctly?

Problem

Create a small project that uses a package.json file and runs a JavaScript program.

Solution

Create a project:

mkdir test-node-app
cd test-node-app
npm init -y

Create app.js:

console.log("Node.js project is working!");

Run:

node app.js

Output

Node.js project is working!

Step-by-Step Explanation

  1. mkdir test-node-app creates the project folder.
  2. cd test-node-app enters the folder.
  3. npm init -y creates package.json.
  4. Create app.js.
  5. Add a simple console.log() statement.
  6. Run the file using node app.js.
  7. If the message appears, your basic Node.js project setup is working.

Question 10: How do you create and run a Node.js project using an npm script?

Problem

Create a project with an npm start script and use it to run your Node.js application.

Solution

Create a project:

mkdir my-app
cd my-app
npm init -y

Create app.js:

console.log("Application started successfully!");

Now open package.json and change the scripts section to:

{
  "scripts": {
    "start": "node app.js"
  }
}

Run:

npm start

Output

Application started successfully!

Step-by-Step Explanation

  1. Create a Node.js project folder.
  2. Run npm init -y.
  3. Create app.js.
  4. Add your JavaScript code.
  5. Add "start": "node app.js" to the scripts section.
  6. Run npm start.
  7. npm executes the command defined inside the start script.
  8. Node.js runs app.js.

npm scripts are useful because they let you run common project commands with short commands.

Key Takeaways

  • Node.js can be checked using node -v or node --version.
  • npm can be checked using npm -v or npm --version.
  • A separate folder should be used for each Node.js project.
  • JavaScript files can be executed using the node command.
  • npm init creates a package.json file.
  • npm init -y creates package.json using default settings.
  • package.json contains important project information.
  • npm install is used to install Node.js packages.
  • Installed packages are normally stored in the node_modules folder.
  • npm scripts can simplify frequently used project commands.
  • A properly configured Node.js environment makes learning backend development much easier.

FAQs

What is required to install Node.js?

You need a supported operating system, internet access for downloading the installer, and sufficient permission to install software on your computer. After installation, you can verify Node.js with the node -v command.

How do I know if Node.js is installed correctly?

Open your terminal and run:

node -v

If a Node.js version is displayed, the node command is available. You can also run npm -v to check npm.

What is npm in Node.js?

npm is the Node Package Manager. It is used to install packages, manage project dependencies, create project configuration files, and run scripts.

What is package.json in Node.js?

package.json is a configuration file for a Node.js project. It can contain the project name, version, scripts, dependencies, and other project information.

What is the node_modules folder?

node_modules is the folder where npm installs packages required by a Node.js project. You generally should not manually edit files inside this folder.

Why is npm install used?

The npm install command downloads and installs packages required by a project. It can also update the project’s dependency information in package.json.

Can I run Node.js without installing it?

Normally, Node.js needs to be installed on your computer to run Node.js applications locally. After installation, the node command becomes available in your terminal.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top