Table of Contents
Node.js is an open-source runtime environment designed for executing server-side JavaScript. It enables the development of fast, lightweight, and scalable web applications. Node.js utilizes the Chrome V8 JavaScript engine, delivering high performance.
Here are the key features and concepts of Node.js:
- Server-Side JavaScript Programming:
- Node.js allows JavaScript to run outside the browser, enabling the use of the same language on both the client and server sides.
- Asynchronous Operations and Event-Driven Model:
- Node.js employs an asynchronous, event-driven model, allowing multiple operations to occur simultaneously and effectively managing input/output processes.
- Modular Structure and Package Management:
- Node.js follows a modular structure. It includes a variety of pre-built modules (packages) that can be used in your code, managed through npm (Node Package Manager).
- Core Modules for Swift Operations:
- Node.js has core modules providing access to the file system, network, HTTP client, and server, facilitating rapid development.
- Wide Community Support and Ecosystem:
- Node.js is supported by a large developer community and a vast ecosystem. This ensures the availability of diverse modules, libraries, and tools.
- Lightweight and Fast:
- Node.js is a lightweight runtime environment and achieves high performance thanks to the V8 engine.
- Cross-Platform Support:
- Node.js can run on different operating systems (Windows, macOS, Linux), providing flexibility for developers to build applications on various platforms.
Node.js is widely used in modern web development scenarios, including real-time applications, APIs, microservices, and single-page applications.
Node.js Installation:
- Update and Upgrade Packages Including Node.js and npm:
sudo apt update sudo apt upgrade
- Add the NodeSource repository to use an up-to-date version of Node.js.Add the NodeSource Repository:
-
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Note: In the above command, change “14.x” to the desired version of Node.js you want to use.
- Install Node.js and npm:
sudo apt install -y nodejs
npm (Node Package Manager) will be installed automatically alongside Node.js.
- Verify the Installation:
- Check the Node.js and npm versions to confirm a successful installation.
node -v npm -v
These steps successfully install Node.js and npm on your Ubuntu system. You can now create and run Node.js projects. Remember to use npm to manage packages in your projects.
Node.js Basics:
- Check Node.js Version:
node -v
- Check npm Version:
npm -v
Creating and Managing Projects:
- Initialize a New Node.js Project:
npm init -y
- Install a Package Locally:
npm install package-name
- Install a Package Globally:
npm install -g package-name
- Install Packages from
package.json
:npm install
Running Node.js Applications:
- Run a Node.js Script:
node filename.js
- Run a Node.js Script with Nodemon (Automatic Reloading):
nodemon filename.js
Package Management:
- List Installed Packages:
npm list
- Search for a Package:
npm search package-name
- Show Package Information:
npm show package-name
Managing Node.js Versions:
- Install Node Version Manager (nvm):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Install a Specific Node.js Version:
nvm install 14.17.5
- Switch Node.js Version:
nvm use 14.17.5
npm Scripts:
- Add Script in
package.json
:"scripts": { "start": "node index.js" }
- Run npm Script:
npm start
Express.js (Web Framework) Basics:
- Install Express:
npm install express
- Create a Simple Express App:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, Express!'); }); app.listen(3000, () => { console.log('Server is running on port 3000'); });
This cheat sheet covers a range of Node.js and npm commands, package management, version management, and basic Express.js usage. For more in-depth information, refer to the Official Node.js Documentation and npm Documentation.