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.
IntroductionNode.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!
PrerequisiteTo check if node.js 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.
Create A New FolderBefore 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.
\
\ 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.
Navigate To The FolderAfter creating a new folder, it doesn't end there. You have to tell your computer to open the folder. To do this**;**
\ 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.
Initialize npmnpm, 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;
\
\ This is the prompt used to start npm in your project manually or automatically.
:::info Note: You must do this every time you build a new project.
:::
Start npm ManuallyTo start npm manually in your project, you do this;
\ After this, you are directed to respond to a few default questions and follow subsequent steps;
\ 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.
:::info 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.
:::
Install DependenciesEvery 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 Transitive (Indirect) DependenciesThese 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.
Development (Dev) DependenciesThese 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" } Peer DependenciesThese 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" } Optional DependenciesDependencies 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" } Bundle DependenciesDependencies bundled with your project rather than installed separately. It is used when you want to include a dependency inside a package.
System DependenciesThese are external software required for your project, such as a database or runtime environment. Example: Node.js, MongoDB, or PostgreSQL are system dependencies.
Managing Dependencies in Node.jsnpm install
Example,
npm install express\
npm install --save-dev
Example,
npm install –save-dev nodemon\
\
npm uninstall
Example,
npm uninstall eslint Production DependenciesThese 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" }\
\
\
:::info Note: Only install the necessary dependency you need for your project to reduce app size and improve performance.
:::
Begin ProjectYou 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;
\ 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.
Write Your Hello WorldBefore 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;
\
\ 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;
\
\ 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,
Open the main file index.js
Type console.log("Hello World")
Save it— use Ctrl S (Windows) or command S (Mac) or click on File at the top of your VS Code editor and click Save.
After saving your file, right-click on the 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
In the terminal, type node index.js and Click Enter. \n
\ This will process your code in the terminal and show that your code is successfully run by node.js.
ConclusionIn 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, check out the node.js documentation.
\ I hope the examples provided helped you understand how to set up node.js to use for your new projects.
All Rights Reserved. Copyright 2025, Central Coast Communications, Inc.