keepcalculator

06 Numbers and Powers

Install Node.js on CentOS: Step-by-Step

Card No. 06·1365 · Updated · Standard formula, shown below

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

  1. 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.
  2. 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.
  3. Verify the installation
    Check the installed version:
    node --version
    You should see something like v10.24.0. Also check npm (Node Package Manager):
    npm --version
    If both commands return version numbers, Node.js is installed correctly.
  4. 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.
  5. Verify the new version
    Run node --version again. It should now show v18.x.x (or whatever version you installed).
  6. Test with a simple script
    Create a test file:
    echo 'console.log("Hello, Node.js!");' > test.js
    Run it:
    node test.js
    You should see Hello, Node.js! printed.
  7. 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.
  8. 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
  9. 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 npm before adding NodeSource.
  • Forgetting to update the package cache – After adding NodeSource, run sudo yum update to 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 nvm if you need to switch between multiple Node.js versions frequently. Install it with curl -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 --permanent and reload.

Troubleshooting

  • “node: command not foundâ€� – This means the installation failed or the PATH is wrong. Reinstall with sudo yum install nodejs and check which 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 $PATH to verify.
  • Version still old after NodeSource install – Remove the old package first, then reinstall. Run sudo yum remove nodejs and then sudo 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

The whole Numbers and Powers drawer →