Understanding Node.js and Its Importance
Node.js has become an essential tool for modern web development. It’s a powerful JavaScript runtime built on Chrome’s V8 JavaScript engine, allowing developers to build scalable network applications. Node.js uses an event-driven, non-blocking I/O model, making it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Why Choose Node.js?
Node.js is chosen for its ability to handle asynchronous operations and its single-threaded nature, which can manage multiple connections simultaneously. It’s also supported by a large and active community, which contributes to a vast ecosystem of packages and modules available through the Node Package Manager (NPM).
Preparing for Node.js Installation on CentOS 7
Before installing Node.js on CentOS 7, it’s important to ensure that your system is up to date. This can be done by running the following commands in the terminal:
yum update
yum upgrade
These commands will update the package index and upgrade the existing packages to their latest versions. It’s also a good practice to have the build tools installed, as they are often required for compiling and installing native addons from NPM.
yum groupinstall 'Development Tools'
Installing Node.js on CentOS 7
Option 1: Using NodeSource Repository
NodeSource provides a repository containing the latest versions of Node.js. To add the NodeSource repository to your system, use the following curl command:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -
After adding the repository, you can install Node.js using the yum package manager:
yum install -y nodejs
This will install Node.js along with NPM, which is the package manager for Node.js modules.
Option 2: Using NVM (Node Version Manager)
NVM is a script-based node version manager that allows you to install and manage multiple Node.js versions. To install NVM, use the following curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
After installing NVM, you can install Node.js by running:
nvm install node
This command will install the latest version of Node.js. To install a specific version, you can run:
nvm install 14.17.0
NVM is particularly useful if you need to switch between different Node.js versions for various projects.
Option 3: Compiling from Source
For those who prefer to compile Node.js from source, you can download the latest source code from the official Node.js website. Here’s how you can compile and install Node.js from source:
curl -sL https://nodejs.org/dist/v14.17.0/node-v14.17.0.tar.gz | tar xz
cd node-v14.17.0
./configure
make
sudo make install
Compiling from source allows you to have more control over the installation process and configuration options.
Verifying Node.js Installation
After installing Node.js, it’s important to verify the installation. You can check the installed version of Node.js and NPM by running:
node -v
npm -v
These commands should output the versions of Node.js and NPM installed on your system, confirming that the installation was successful.
Managing Node.js Packages with NPM
NPM is the default package manager for Node.js and is used to install, share, and manage dependencies in your projects. Here’s how you can use NPM to install a package:
npm install express
This command will install the Express.js framework, which is a popular web application framework for Node.js. You can also install packages globally using the -g flag:
npm install -g nodemon
Global packages are typically command-line tools that you can use anywhere on your system.
Setting Up a Basic Node.js Application
To start a new Node.js project, create a directory for your project and initialize it with a package.json file by running:
mkdir my-node-app
cd my-node-app
npm init -y
The package.json file will include metadata about your project and the list of dependencies. You can then create an index.js file as your entry point and start writing your Node.js application.
Configuring Your Node.js Environment
For a more robust development environment, you can use environment variables to store configuration settings. The dotenv package is commonly used for this purpose. Install it using NPM:
npm install dotenv
Create a .env file in your project root and add your configuration settings:
PORT=3000
DB_HOST=localhost
In your Node.js application, you can then access these variables using process.env:
require('dotenv').config();
const port = process.env.PORT;
Securing Your Node.js Application
Security is paramount when developing web applications. Use packages like helmet to help protect your app from some well-known web vulnerabilities by setting HTTP headers appropriately.
npm install helmet
In your application, use helmet as middleware:
const helmet = require('helmet');
app.use(helmet());
Additionally, regularly update your dependencies and audit your Node.js applications for vulnerabilities using the npm audit command.
Deploying Your Node.js Application
When you’re ready to deploy your Node.js application, you can use services like PM2 to manage and keep your app running without downtime. PM2 is a production process manager for Node.js applications with a built-in load balancer.
npm install pm2 -g
pm2 start index.js
PM2 will now manage your Node.js application, ensuring that it’s restarted if it crashes and facilitating common system admin tasks.
Frequently Asked Questions
How do I update Node.js on CentOS 7?
To update Node.js, you can use NVM to install a newer version or update the NodeSource repository and run yum update nodejs.
Can I install multiple versions of Node.js on CentOS 7?
Yes, you can use NVM to install and manage multiple versions of Node.js on the same system.
How do I uninstall Node.js from CentOS 7?
To uninstall Node.js, you can run yum remove nodejs if you installed it via the package manager or nvm uninstall if you used NVM.
Is Node.js free to use?
Yes, Node.js is open-source and free to use under the MIT license.
What is the difference between Node.js and NPM?
Node.js is a runtime environment for executing JavaScript code on the server side, while NPM is a package manager for managing Node.js modules.