Skip to main content

Installing Node.js on OpenSUSE

Hello welcome to our lesson on installing Node.js on OpenSUSE. Node.js is a powerful and widely-used JavaScript runtime that allows developers to build scalable network applications. It's known for its non-blocking I/O model, which makes it lightweight and efficient, ideal for data-intensive real-time applications that run across distributed devices.

OpenSUSE, with its robust and flexible package management system, provides an excellent foundation for Node.js development. Whether you're a seasoned developer or just starting out, this guide will walk you through the process of setting up Node.js on your OpenSUSE system step by step.

Prerequisites

Before we begin, ensure that you have the following:.

  • An OpenSUSE system. This guide is applicable to both Leap and Tumbleweed editions.
  • A user account with sudo privileges or access to a user with such privileges.
  • Basic familiarity with the terminal and command-line operations.

Updating the System

It's always a good idea to start with an updated system. Open a terminal and run the following commands to refresh your package repositories and upgrade the installed packages:.

sudo zypper refresh
sudo zypper update

Installation Methods

There are several methods to install Node.js on OpenSUSE. We'll explore the most common approaches:.

Using the Official OpenSUSE Repositories

The official OpenSUSE repositories typically contain a version of Node.js, though it might not be the latest. To install it, execute:.

sudo zypper install nodejs npm

After the installation, you can verify the Node.js and npm versions by running:.

node --version
npm --version

Using Node Version Manager (NVM)

NVM allows you to manage multiple Node.js versions on the same system. To install NVM, follow these steps:.

  1. Download and run the NVM installation script:.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
  1. Close and reopen your terminal, or source your profile file with source ~/.profile or source ~/.bashrc.

  2. Verify the NVM installation:.

nvm --version

My Output:.

0.39.1
  1. Install the latest version of Node.js:.
nvm install node
  1. Set the latest version as the default:.
nvm alias default node
  1. Verify the Node.js and npm versions:.
node --version
npm --version

Using NodeSource Repository

NodeSource provides a repository containing the latest versions of Node.js. Here's how to use it:.

  1. Choose the version of Node.js you want to install from the NodeSource repository.

  2. Import the NodeSource signing key:.

curl -sL https://rpm.nodesource.com/setup_X.X.x | sudo bash -

Replace X.X.x with the version number you've chosen.

  1. Install Node.js:.
sudo zypper install nodejs
  1. Verify the installation:.
node --version
npm --version

Managing Node.js Versions with NVM

If you installed Node.js using NVM, you can easily switch between versions. Here's how:.

  • To list installed versions:.
nvm ls
  • To switch to a specific version:.
nvm use vX.X.X
  • To set a default Node.js version:.
nvm alias default vX.X.X

Configuring npm

After installing Node.js and npm, you might want to configure npm to suit your needs. Here are some common configurations:.

  • To set the default registry (useful if you're behind a proxy):.
npm config set registry http://registry.npmjs.org/
  • To change the directory where npm installs global packages:.
npm config set prefix /path/to/custom/location
  • To fix npm permissions issues on a global level:.
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

Installing Global Packages

To install packages globally with npm, use the -g flag:.

sudo npm install -g package-name

For example, to install the nodemon utility globally:.

sudo npm install -g nodemon

Setting Up a Node.js Project

To start a new Node.js project, follow these steps:.

  1. Create a new directory for your project:.
mkdir my-node-project && cd my-node-project
  1. Initialize a new Node.js project:.
npm init -y

This will create a package.json file with default values.

  1. Add dependencies to your project:.
npm install express

This will install the express package and add it to the dependencies section of your package.json file.

Security Considerations

Security is paramount when developing applications. To ensure your Node.js project's dependencies are secure, use:.

npm audit

This command will analyze your project for vulnerabilities and suggest fixes.

Troubleshooting Common Issues

  • Node.js or npm not found: Ensure that Node.js and npm are installed correctly and that their paths are included in your system's PATH environment variable.
  • Permission denied when installing global packages: Use sudo when installing global packages, or configure npm to install them in a directory you own.
  • npm audit reports vulnerabilities: Update the affected packages with npm update or address them individually with npm install package-name@secure-version.

You've successfully learned how to install Node.js on OpenSUSE using different methods. Whether you chose the official repositories, NVM, or the NodeSource repository, you're now equipped to start building your Node.js applications.

Further Reading

Appendix: Uninstalling Node.js

If you need to uninstall Node.js from your OpenSUSE system, use the following command:.

sudo zypper remove nodejs npm

If you installed Node.js with NVM, you can uninstall a specific version with:.

nvm uninstall vX.X.X

To completely remove NVM, delete the lines added to your profile file (~/.profile, ~/.bashrc, etc.) and remove the NVM directory:.

rm -rf ~/.nvm

We hope this guide has been helpful. If you encounter any issues or have suggestions, please leave a comment below or reach out to the OpenSUSE community forums. Happy coding!