This guide walks you through installing Node.js on CentOS, covering both the stable release via the official repository and the latest version using NodeSource. You’ll learn how to set up Node.js, verify the installation, and manage it with npm, giving you a working JavaScript runtime for server-side applications.
What You’ll Need
- A CentOS server or virtual machine (version 7 or later) with sudo or root access.
- An internet connection to download packages.
- Basic familiarity with the command line.
Step-by-Step
- Update your system packages
Open a terminal and run:
sudo yum update -y
This ensures all existing packages are up to date, reducing the chance of conflicts. - Install Node.js from the CentOS repository (simplest method)
CentOS includes a Node.js package in its default repositories. Install it with:
sudo yum install nodejs -y
This installs the version bundled with your CentOS release (often Node.js 10 or 12 on CentOS 7/8). It’s stable but may be older. - Verify the installation
Check the installed version:
node --version
You should see something likev10.24.0. Also check npm (Node Package Manager):
npm --version
If both commands return version numbers, Node.js is installed correctly. - Install a newer version using NodeSource (recommended for modern apps)
If you need a recent Node.js version, add the NodeSource repository. For Node.js 18 (LTS), run:
curl -sL https://rpm.nodesource.com/setup_18.x | sudo bash -
This script adds the repository and updates your package list. Then install:
sudo yum install nodejs -y
This replaces the older version with Node.js 18. - Verify the new version
Runnode --versionagain. It should now showv18.x.x(or whatever version you installed). - Test with a simple script
Create a test file:
echo 'console.log("Hello, Node.js!");' > test.js
Run it:
node test.js
You should seeHello, Node.js!printed. - Install build tools (optional but recommended)
Many npm packages require compiling native code. Install the necessary tools:
sudo yum groupinstall 'Development Tools' -y
This installs GCC, make, and other compilers. - Set up npm global directory (optional)
By default, npm installs global packages to a system directory that requires sudo. To avoid permission issues, change the prefix to your home directory:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Then add it to your PATH by editing~/.bashrc:
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc - Update npm to the latest version
npm often lags behind the latest. Update it with:
npm install -g npm@latest
Common Mistakes
- Mixing yum and NodeSource versions – If you install from the default repo first, then add NodeSource, the new install may not overwrite the old one. Always remove the old package first:
sudo yum remove nodejs npmbefore adding NodeSource. - Forgetting to update the package cache – After adding NodeSource, run
sudo yum updateto refresh the metadata, otherwise yum might not see the new packages. - Using sudo with npm global installs – This can create permission problems later. Use the prefix method described above.
- Ignoring the development tools – Many packages fail to install without them. Install the group before attempting to npm install packages with native code.
Tips
- Use a version manager like
nvmif you need to switch between multiple Node.js versions frequently. Install it withcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash. - Always test your Node.js installation with a simple script before deploying a real application.
- Keep Node.js updated regularly. Check the official Node.js website for the latest LTS version.
- If you’re using a firewall, ensure port 3000 (or your app’s port) is open:
sudo firewall-cmd --add-port=3000/tcp --permanentand reload.
Troubleshooting
- “node: command not found� – This means the installation failed or the PATH is wrong. Reinstall with
sudo yum install nodejsand checkwhich node. - “npm: command not foundâ€� – npm is installed with nodejs, but if you’re using a custom prefix, ensure the PATH is set correctly. Run
echo $PATHto verify. - Version still old after NodeSource install – Remove the old package first, then reinstall. Run
sudo yum remove nodejsand thensudo yum install nodejs. - “curl: command not found� – Install curl with
sudo yum install curl. - Permission errors when installing global packages – Use the npm prefix method or run with
sudo(not recommended). - Compilation errors when installing a package – Install the Development Tools group as described in step 7, then try again.
Frequently Asked Questions
Can I install Node.js without sudo?
No, the system-wide installation requires root privileges. However, you can use nvm to install Node.js in your home directory without sudo.
What is the difference between Node.js and npm?
Node.js is the runtime that executes JavaScript code. npm is a package manager that installs and manages libraries and dependencies for your projects.
Should I use the CentOS repo version or NodeSource?
If you need a stable, older version and don’t mind limited updates, use the CentOS repo. For modern features and security patches, use NodeSource.
How do I uninstall Node.js?
Run sudo yum remove nodejs npm. If you used NodeSource, also remove the repo file: sudo rm /etc/yum.repos.d/nodesource*.repo.
Can I install a specific version like Node.js 16?
Yes, change the setup script URL to setup_16.x for Node.js 16. NodeSource supports major versions 14, 16, 18, and 20.
How do I check if Node.js is working correctly?
Run node -e "console.log('ok')" – it should print ok. Also check npm -v for npm.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Mac Start Up Automatically After Power Failure?
- Expose Self-Hosted Service Securely Without Cloudflare Tunnel
- Nano Delivery for Functional Foods: What It Can and Can’t Do4.00
- Disable Apple Watch Turn-by-Turn Vibration While Driving
- Convert Play FLAC in iTunes: Full Guide
- Soda Foundation Charter: What It Means