You can follow the following steps to create a Node.js project:
1. Install Node.js:
If Node.js is not installed in your computer , download and install it from it's official website.
2. Initialize the Project:
Open a terminal and create a new directory for your project:
mkdir project-namecd project-name
Initialize a new Node.js project by running:
npm init
This command will ask a series of questions (like project name, version, description, etc.). If you want to skip the questions, use:
npm init -y
Now a package.json file is created which will keeps track of your project's dependencies and configurations.
3. Install Dependencies (Optional):
Install any libraries you need, such as express for creating a server:
npm install express
4. Create the Main JavaScript File:
Create a server.js (or another name) file in the project folder:
touch server.js
Add the following example code to create a basic server using express:
const express = require('express');
const app = express();
const port = 5000; app.get('/', (req, res) => { res.send('Hi, Eduerka!');});app.listen(port, () => { console.log(`Server running at http://localhost:${port}`);});
5. Run the Project:
Start the server by running:
node server.js
You should see a message: Server running at http://localhost:5000.