Getting Started with Node.js and npm
Learn the basics of Node.js and npm, and create your first project.
Introduction to Node.js
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine that allows developers to create scalable and high-performance server-side applications. To get started with Node.js, you need to have it installed on your computer.
Installing Node.js and npm
You can download the latest version of Node.js from the official website. Once installed, you can verify that Node.js and npm (the package manager that comes with Node.js) are working by running the following commands in your terminal:
node -v
npm -v
Creating a New Project
To create a new project, navigate to the directory where you want to create your project and run the following command:
npm init
This will prompt you to enter some information about your project, such as its name, version, and description. Once you've entered this information, a package.json file will be created in your project directory.
Installing Dependencies
To install dependencies for your project, you can use npm. For example, to install the Express.js framework, you can run the following command:
npm install express
Creating a Server
To create a server, you can use the following code:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});
You can run this code by saving it to a file called server.js and running the following command:
node server.js
You can then access your server by navigating to http://localhost:3000 in your web browser.