In this section, we will guide you through the process of setting up Node.js on your machine. By the end of this module, you will have Node.js installed and ready to run your first program.
- Checking for Node.js Installation
Before installing Node.js, it's a good idea to check if you already have it installed on your system. Open your terminal or command prompt and type the following command:
If Node.js is installed, you will see the version number. If not, you will get an error message indicating that the command is not recognized.
- Downloading Node.js
To download Node.js, follow these steps:
-
Visit the Node.js website: Go to https://nodejs.org/.
-
Choose the appropriate version: You will see two versions available for download:
- LTS (Long Term Support): Recommended for most users, especially for production environments.
- Current: Includes the latest features but may not be as stable as the LTS version.
-
Download the installer: Click on the version you want to download. This will download the installer for your operating system (Windows, macOS, or Linux).
- Installing Node.js
Windows
- Run the installer: Double-click the downloaded
.msi
file to start the installation process. - Follow the prompts: The installer will guide you through the installation steps. Make sure to check the box that says "Automatically install the necessary tools" if prompted.
- Verify the installation: Open a new command prompt and type
node -v
to check the installed version.
macOS
- Run the installer: Double-click the downloaded
.pkg
file to start the installation process. - Follow the prompts: The installer will guide you through the installation steps.
- Verify the installation: Open a new terminal window and type
node -v
to check the installed version.
Linux
For Linux, you can use a package manager to install Node.js. Here are the commands for some popular distributions:
Ubuntu/Debian
CentOS/RHEL
Fedora
After installation, verify the installation by typing node -v
in your terminal.
- Installing Node.js via nvm (Node Version Manager)
Using nvm is a great way to manage multiple versions of Node.js on your machine. Here’s how to install and use nvm:
Installing nvm
-
Download and install nvm: Open your terminal and run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
-
Load nvm: Add the following lines to your
.bashrc
,.zshrc
, or.profile
file:export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
-
Restart your terminal: Close and reopen your terminal or run
source ~/.bashrc
(or the appropriate file for your shell).
Using nvm
-
Install a specific version of Node.js: For example, to install Node.js version 14, run:
nvm install 14
-
Use a specific version of Node.js: To switch to the installed version, run:
nvm use 14
-
Verify the installation: Check the installed version by running:
node -v
- Installing npm (Node Package Manager)
npm is automatically installed with Node.js. You can verify the installation by running:
Conclusion
You have now successfully set up Node.js on your machine. In the next section, we will write and run your first Node.js program. This setup will serve as the foundation for all the upcoming modules in this course. Happy coding!
Node.js Course
Module 1: Introduction to Node.js
Module 2: Core Concepts
Module 3: File System and I/O
Module 4: HTTP and Web Servers
Module 5: NPM and Package Management
- Introduction to NPM
- Installing and Using Packages
- Creating and Publishing Packages
- Semantic Versioning
Module 6: Express.js Framework
- Introduction to Express.js
- Setting Up an Express Application
- Middleware
- Routing in Express
- Error Handling
Module 7: Databases and ORMs
- Introduction to Databases
- Using MongoDB with Mongoose
- Using SQL Databases with Sequelize
- CRUD Operations
Module 8: Authentication and Authorization
Module 9: Testing and Debugging
- Introduction to Testing
- Unit Testing with Mocha and Chai
- Integration Testing
- Debugging Node.js Applications
Module 10: Advanced Topics
Module 11: Deployment and DevOps
- Environment Variables
- Using PM2 for Process Management
- Deploying to Heroku
- Continuous Integration and Deployment