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
- Open your terminal.
- Type
node --version. - Press Enter.
- If Node.js is installed, its version will be displayed.
- 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
- Open your terminal.
- Type
npm --version. - Press Enter.
- npm displays its installed version.
- 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
node -vdisplays the Node.js version.npm -vdisplays the npm version.- Both commands help you confirm that your Node.js development environment is working.
- 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
mkdir my-node-appcreates a new folder.cd my-node-appmoves into that folder.- 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
- Create a project folder.
- Create a file named
app.js. - Write JavaScript code inside the file.
- Open the terminal in the project folder.
- Run
node app.js. - Node.js executes the JavaScript code.
- 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
- Open the terminal inside your project folder.
- Run
npm init -y. - npm creates a
package.jsonfile. - This file stores important information about your Node.js project.
- 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
- Open your project folder.
- Run
npm init. - npm starts the project setup process.
- Enter your preferred package name.
- Provide other information or accept the defaults.
- npm creates the
package.jsonfile.
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
- Open your Node.js project folder.
- Run
npm install lodash. - npm downloads the package.
- npm creates or updates the
node_modulesfolder. - npm records the package in
package.json. - 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
mkdir test-node-appcreates the project folder.cd test-node-appenters the folder.npm init -ycreatespackage.json.- Create
app.js. - Add a simple
console.log()statement. - Run the file using
node app.js. - 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
- Create a Node.js project folder.
- Run
npm init -y. - Create
app.js. - Add your JavaScript code.
- Add
"start": "node app.js"to thescriptssection. - Run
npm start. - npm executes the command defined inside the
startscript. - 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 -vornode --version. - npm can be checked using
npm -vornpm --version. - A separate folder should be used for each Node.js project.
- JavaScript files can be executed using the
nodecommand. npm initcreates apackage.jsonfile.npm init -ycreatespackage.jsonusing default settings.package.jsoncontains important project information.npm installis used to install Node.js packages.- Installed packages are normally stored in the
node_modulesfolder. - 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.
