Node.js and npm Fundamentals
1. Introduction to Node.js
Node.js is an open-source, cross-platform JavaScript runtime environment that allows developers to run JavaScript code outside of a web browser. It is built on the V8 JavaScript engine, which is used in Google Chrome. Node.js is designed for building scalable network applications and is widely used for server-side development.
Key Points:
- Node.js uses an event-driven, non-blocking I/O model, which makes it lightweight and efficient.
- It is commonly used for building back-end services like APIs, real-time applications, and microservices.
- Node.js uses npm (Node Package Manager) for managing packages and dependencies.
2. Installing Node.js
You can install Node.js using different methods. One of the easiest ways to install Node.js on Linux is by using the package manager.
Install Node.js on Debian/Ubuntu:
sudo apt updatesudo apt install nodejssudo apt install npm
Verify Installation:
node -vnpm -v
This will output the version numbers of Node.js and npm installed.
3. npm (Node Package Manager)
npm is the default package manager for Node.js, and it allows you to install, share, and manage packages for your Node.js projects.
Basic npm Commands:
Install a Package Globally
sudo npm install -g package-name
Install a Package Locally
npm install package-name
Uninstall a Package Globally
sudo npm uninstall -g package-name
Uninstall a Package Locally
npm uninstall package-name
List Installed Packages
npm list
Create a package.json
File
To initialize a new Node.js project and create a package.json
file, run:
npm init
This will prompt you to fill out some details about the project.
Update npm
To update npm to the latest version, use the following command:
npm install -g npm@latest
4. Introduction to nvm (Node Version Manager)
nvm is a tool that allows you to manage multiple versions of Node.js on the same machine. It is useful when you need to switch between different versions of Node.js for different projects.
Install nvm on Debian/Ubuntu:
- Install the required dependencies:
sudo apt updatesudo apt install curl
- Install nvm by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Restart your terminal or source your profile:
source ~/.bashrc
- Verify nvm installation:
nvm --version
Using nvm to Install and Manage Node.js Versions:
Install a Specific Version of Node.js
nvm install 16.13.0
Use a Specific Version of Node.js
nvm use 16.13.0
List Installed Node.js Versions
nvm ls
Set Default Node.js Version
nvm alias default 16.13.0
5. Introduction to pnpm (Performant npm)
pnpm is an alternative to npm that is faster and more efficient. It uses a unique node_modules layout that stores dependencies in a single place, which reduces disk space usage and speeds up installations.
Install pnpm:
To install pnpm, you can use npm (once npm is installed):
npm install -g pnpm
Basic pnpm Commands:
Install Dependencies Using pnpm
pnpm install
Install a Package
pnpm add package-name
Uninstall a Package
pnpm remove package-name
Update a Package
pnpm update package-name
List Installed Packages
pnpm list
Create a New Project with pnpm
pnpm init
6. Conclusion
Node.js, npm, nvm, and pnpm are essential tools for modern JavaScript development. Node.js allows for building server-side applications, while npm and pnpm help manage dependencies efficiently. nvm is particularly useful for managing multiple versions of Node.js. By mastering these tools, you’ll be able to build scalable and efficient applications with ease.
Happy coding!