DEV Community

Olatunji Ayodele Abidemi
Olatunji Ayodele Abidemi

Posted on

To create a new Node.js project and work with dependencies, you'll need to follow these steps

Initialize a new Node.js project: This will create a package.json file in your project directory.

mkdir my-node-project
cd my-node-project
npm init -y
Enter fullscreen mode Exit fullscreen mode

Install dependencies: Use npm to install any libraries you need. For example, if you need Express.js, you would run:

npm install express
Enter fullscreen mode Exit fullscreen mode

Create your main file: Typically, this is index.js or app.js. Here's a simple Express server

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello World!');
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});
Enter fullscreen mode Exit fullscreen mode

Run your application: You can start your Node.js application with the following command:

node index.js
Enter fullscreen mode Exit fullscreen mode

Remember to replace express with any other dependencies you might need for your project. Also, you can add scripts to your package.json to streamline the start-up process, like so:

"scripts": {
  "start": "node index.js"
}
Enter fullscreen mode Exit fullscreen mode

Now, you can start your server with npm start. This is a basic setup, and your actual implementation might vary based on the project requirements.

Top comments (0)