WindowsMacSoftwareSettingsSecurityAndroidProductivityLinuxPerformanceAppleDevice Manageme.. All

How to Install and Configure Node.js on Fedora

Edited 2 weeks ago by ExtremeHow Editorial Team

FedoraNode.jsJavaScriptInstallationConfigurationDevelopmentCommand LineTerminalSoftwareProgramming

How to Install and Configure Node.js on Fedora

This content is available in 7 different language

Node.js is a JavaScript runtime environment that is widely used to build scalable and efficient applications. Whether you are a beginner who wants to get started with Node.js on your Fedora machine or an experienced developer who needs to set up a new system, this detailed guide will walk you through the entire process of installing and configuring Node.js on Fedora. We will cover different installation methods, and by the end of this guide, you should be able to run your own Node.js applications without any problems.

Why install Node.js on Fedora?

Fedora is a popular Linux distribution that offers a cutting-edge platform with regular updates and a commitment to open-source software. By installing Node.js on Fedora, developers can leverage its robust features to build and deploy high-performance applications in a secure and stable environment. Node.js allows you to execute JavaScript on the server-side, opening up a wealth of possibilities for developing server applications such as web servers, APIs, or any programmable event-based server.

Prerequisites

Before starting the installation of Node.js, you need to make sure that your Fedora system is up to date. You can update your system by running the following command:

sudo dnf update

This command will refresh your package index and upgrade all installed packages to their latest available version. This process ensures that your system is running optimally and can prevent potential problems during the Node.js installation process.

Installing Node.js

There are multiple ways to install Node.js on Fedora. We will discuss some of the common methods in detail. Choose the method that best suits your needs.

Method 1: Using the DNF package manager

Fedora's default package manager, DNF, can be used to install Node.js. This method is straightforward and keeps Node.js up to date whenever you update your system. Follow these steps:

  1. Open a terminal window.
  2. Enable the Node.js module stream by running the command below. This will list all the available versions of Node.js:
  3. sudo dnf module list nodejs
  4. Choose a version to install. Generally, you may need the latest stable release. For example, to install Node.js 16:
  5. sudo dnf module install nodejs:16
  6. Verify the installation by checking the installed version of Node.js:
  7. node -v
  8. Check npm (the Node Package Manager), which is installed with Node.js:
  9. npm -v

This method ensures that you're installing a version of Node.js that has been tested for stability and compatibility with Fedora's ecosystem.

Method 2: Using Node Version Manager (NVM)

Another popular way to install Node.js is through Node Version Manager (NVM). It lets you easily manage multiple Node.js versions on the same system. This method is very flexible if you frequently switch between different Node.js projects that require different versions. Follow these steps to install Node.js using NVM:

  1. First, make sure you have the necessary development tools. Install these using:
  2. sudo dnf install curl
  3. Download the NVM installation script:
  4. curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  5. Activate NVM by sourcing your shell's startup script. Let's say it's bash:
  6. source ~/.bashrc
  7. Verify the NVM installation:
  8. nvm --version
  9. Now, install Node.js using NVM. For example, to install the latest version:
  10. nvm install node
  11. Or, install a specific version, such as Node.js 16:
  12. nvm install 16
  13. Switch between installed Node.js versions:
  14. nvm use 16
  15. Set the default Node.js version:
  16. nvm alias default 16
  17. Verify the currently active version:
  18. node -v

NVM is highly beneficial when you are developing with multiple node environments or when you want a sandboxed version.

Method 3: Using binary packages

If you need or want to use specific versions that are not available in your DNF repository or through NVM, you can download binary packages directly from the Node.js website.

  1. Visit the official website of Node.js.
  2. Download Linux Binaries (.tar.xz) for your target Node.js version.
  3. Extract the archive to your favorite directory:
  4. tar -xf node-vxx.xx.x-linux-x64.tar.xz
  5. Add Node.js to your PATH. Let's say you've extracted it to /opt/node-vxx.xx.x;
  6. export PATH=/opt/node-vxx.xx.x/bin:$PATH
  7. For permanent PATH modification, add the above line to your shell's RC file such as ~/.bashrc.
  8. Check node and npm version:
  9. node -v
    npm -v

The binary method gives you control over specific Node.js versions and can be useful for deployments where you want full control over the runtime environment.

Configuring Node.js

Once Node.js is installed, you may want to configure it to suit your development workflow. Here are some basic configurations you can consider:

1. Global npm package

NPM is the package manager for Node.js. To avoid permission issues, configure a directory for global installation. The following steps guide you through setting it up:

  1. Create a new directory for your global packages:
  2. mkdir ~/.npm-global
  3. Configure npm to use this new directory path:
  4. npm config set prefix '~/.npm-global'
  5. Modify the PATH variable to include the binary from this newly created directory. Add these lines to your shell's startup script:
  6. export PATH=~/.npm-global/bin:$PATH
  7. Update changes by specifying the source of the profile:
  8. source ~/.bashrc

2. Interactive REPL environment

Node.js comes with an interactive REPL (Read-Eval-Print-Loop) environment that can evaluate expressions, run scripts, and perform debugging. To start the REPL environment, simply type:

node

You can now write JavaScript code directly in your terminal and evaluate its results instantly.

3. Creating a simple Node.js application

Let's create a simple Node.js application to verify that everything is set up correctly. Follow these steps:

  1. Create a new directory for your application:
  2. mkdir my-node-app && cd my-node-app
  3. Create a package.json file that contains various metadata related to your project:
  4. npm init -y
  5. Create an index.js file and add the following code:
  6. const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World! This is Node.js running on Fedora'); }); server.listen(3000, '127.0.0.1', () => { console.log('Server running at http://127.0.0.1:3000/'); });
  7. Run your Node.js application:
  8. node index.js
  9. Open a web browser and go to http://127.0.0.1:3000, where you will see "Hello, World! This is Node.js running on Fedora."

This simple application sets up a basic HTTP server, responds to requests, and demonstrates the power of Node.js to handle such tasks efficiently.

Troubleshooting common problems

During installation or configuration, you may encounter several problems. Below are solutions to common problems:

Conclusion

Node.js is a versatile runtime environment that can greatly simplify the development and deployment of JavaScript applications. Fedora provides a solid platform for running such applications, promising a stable, secure, and cutting-edge environment. Whether through a package manager like DNF, a version manager like NVM, or direct binary installation, you have many options to suit your preferences and needs.

By following this comprehensive guide, you should now have Node.js up and running on your Fedora machine, as well as the knowledge to configure it to suit your development workflow. Enjoy creating innovative applications.

If you find anything wrong with the article content, you can


Comments