Building the server side of an application, there are different programming languages to use, and one of them is JavaScript. Originally, JavaScript was built to focus on the client side of web applications. Later on, JavaScript became useful in server-side development. Some companies, to this day, use JavaScript for their server-side development.
Node.js is an open-source JavaScript runtime that uses the V8 JavaScript engine for developing server-side and networking apps/APIs. It allows the use of JavaScript to write code outside the browser like other server-side programming languages like Python, C#, PHP, etc.
In this article, we will cover how to set up node.js to kickstart your project. Assuming you are a backend developer tasked with a project to log a message— “Hello World”. This is where this tutorial will come in handy. Let's get started!
To check if node.js is installed on your computer;
node -v
or node - - version
. This will show you the version of Node.js that is installed on your computer.
Here is an example:
node -v
Here is the output:
v23.6.0
This tells you that version 23.6.0 of node.js is successfully installed on your computer.
Before working on your project, you need a folder. This is where your project is stored locally on your computer. It helps to keep your files organized when you are working on different projects. You have installed node.js. The next is creating a folder.
mkdir
Example
mkdir my-project
In this example, you are using the prompt mkdir
to tell your computer that you want to create a folder. And, you followed it with the name you chose to name the folder— test-project
Note: You may name the folder any name you prefer.
After clicking the Enter key, your folder is created and stored locally on your computer.
After creating a new folder, it doesn't end there. You have to tell your computer to open the folder. To do this**;**
cd
Example
cd my-project
This shows the file path and the new folder is open and ready for use.
Here is the detailed code:
mkdir my-project
cd my-project
The same method applies to Mac and Linux.
npm, also known as Node Package Manager, is referred to as a library or tool that comes with Node.js. If you don't have node.js installed on your computer, you won't have access to it.
Npm is used for Installing, updating, and removing packages (also known as dependencies) from your project. These packages could be frameworks (express.js) and other tools like nodemon
, dotenv
, etc. needed for a specific project.
It simplifies adding functionality to your project without writing everything from scratch. You have installed node.js and created a folder. The next will be initializing your project;
npm init
or npm init -y
This is the prompt used to start npm in your project manually or automatically.
Note: You must do this every time you build a new project.
To start npm manually in your project, you do this;
npm init
After this, you are directed to respond to a few default questions and follow subsequent steps;
index.js
file is the main file of your project. No editing is required, click Enter
After setting up the npm, a preview is shown to you, and you will be asked, "Is this OK?" Type YES and that's it. After typing “yes”, a package.json file is created in the test-project folder, and you can view it in a code editor.
If you don't want to go through setting up npm manually, you can do it automatically by entering npm init -y
in the command line terminal, and it creates a package.json
file.
The only difference is, that you are not setting up the default settings yourself like you did in the manual method, and you are adding "-y" with the npm init
this time. You will get the same results for both methods.
Note: You are allowed to edit the package.json file after it's created. Go to the file on your code editor and make your edits.
Every dependency (package) you need for a project is always obtained from the npm. There are various types of dependencies:
These are explicitly installed and used in your project. Example: If your project uses Express.js, then Express is a direct dependency.
npm install express
These are dependencies of your dependencies. Example: If your project depends on Express.js, and Express internally depends on body-parser, then body-parser is a transitive dependency.
These are only needed for development and testing, but not in production. Example: Libraries like Nodemon, Jest, or linters like ESLint.
In Node.js, these are installed using:
npm install --save-dev nodemon jest eslint
Example package.json snippet after installation
"devDependencies": {
"jest": "^29.0.0",
"eslint": "^8.0.0",
"nodemon": "^3.0.0"
}
These are dependencies that your project requires but expects the user to install. Example: Some middleware packages (e.g., express-session, cors) expect express to be installed separately.
Defined in package.json like this:
"peerDependencies": {
"express": "^4.0.0"
}
Dependencies that are not required but can be installed if available. Example: A project might support both SQLite and PostgreSQL, but only one is necessary.
In package.json, they look like:
"optionalDependencies": {
"sqlite3": "^5.0.2"
}
Dependencies bundled with your project rather than installed separately. It is used when you want to include a dependency inside a package.
These are external software required for your project, such as a database or runtime environment. Example: Node.js, MongoDB, or PostgreSQL are system dependencies.
npm install <package-name>
Example,
npm install express
npm install --save-dev <package-name>
Example,
npm install –save-dev nodemon
npm list //it shows you the list of packages you installed
npm uninstall <package-name>
Example,
npm uninstall eslint
These are essential packages required for your Node.js application to run in a live environment. These dependencies provide core functionalities such as handling requests, connecting to databases, and managing authentication.
They are installed normally with:
npm install express mongoose dotenv axios
And, they are listed in the "dependencies" section of package.json:
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.0.0",
"jsonwebtoken": "^9.0.0",
"dotenv": "^16.0.0"
}
Note: Only install the necessary dependency you need for your project to reduce app size and improve performance.
You have installed node.js, created a folder, and set up npm. It's time to log your first "Hello World" in a code editor. To do this;
code .
code .
This takes you to the code editor (VS Code editor) and you will see the "test-project" folder you created and the package.json file. The package.json file shows you the npm default settings you initially installed into your project.
Before you start, you need a file to write your code. For now, you only have the package.json file, which was created after setting up your npm. If you need any framework or tool for your project, it will be installed in the package.json file. Your code is not written in this file, you have to create another file for writing your code.
During the default settings, the entry point indicated a main file called index.js
. This is the main file of your project and this is where all your code should be written. It should not be written outside this file unless your code won't work.
To open the main file as indicated in your package.json file, you will create a file called "index.js" first (Ensure it matches the name of the main file in the package.json file). To do this;
index.js
And the index.js file is created.
Before you proceed in writing your code, you have to set up a "start" script in the package.json file. This is important because whenever you write a code in the main file (index.js), the runtime (node.js) needs to be initiated to process your code. Without this, your code won't work. To do this;
package.json
file"test: echo/Error: no test specified && exit 1"
"start": "index.js"
The "start" script allows you to use the prompt "node index.js" in your terminal whenever you want to process your code.
For example,
console.log("Hello World")
To do this,
index.js
console.log("Hello World")
index.js
file located at the left-hand side of the VS Code editor and click on Open integrated terminal— this opens the terminal in the main file index.js
node index.js
and Click Enter.
This will process your code in the terminal and show that your code is successfully run by node.js.
In this guide, you get a beginner-friendly and in-depth overview of how to set up your node.js projects. We discussed important steps like setting up npm and installing dependencies.
Remember, you will become a better backend developer when you keep practicing. To learn node.js for building projects,
I hope the examples provided helped you understand how to set up node.js to use for your new projects.