How to Install Node.js and NPM on Debian 12

Updated on September 11, 2024
How to Install Node.js and NPM on Debian 12 header image

Introduction

Node.js is an open-source, cross-platform JavaScript runtime environment for developing web applications. The Node Package Manager (NPM) builds and manages dependency packages for Node.js. Node.js and NPM allow you to use JavaScript for server-side scripting.

This article explains how to install Node.js and NPM on Debian 12. You will install Node.js and manage multiple versions using the NVM (Node Version Manager).

Prerequisites

Before you begin:

Choose a Node.js Version

The Node.js release cycle includes a new version that enters the Current release status for six months. Even-numbered versions transition to an Active LTS (Long-Term Support) status that lasts for 12 months followed by a maintenance status for 18 months. When selecting a Node.js version, consider the release stability, performance, features, and compatibility with your project environment.

The following Node.js releases are available when selecting a version:

  • Current Release: Includes the latest updates, improvements, and features. It's ideal for applications that require new features.
  • LTS Release: Marks a stable Node.js version supported for an extended period and receives continuous feature updates. LTS releases are suitable for production environments because they guarantee critical fixes to common bugs.
  • Previous Releases: Enables compatibility with legacy projects.

Install Node.js and NPM

Node.js is available in the default package repositories on Debian 12, but the available version might be out of date. Follow the steps below to enable the NodeSource PPA repository and install Node.js:

  1. Download the latest Nodesource PPA repository script for your target Node.js version. For example, Node.js version 22.x.

    console
    $ curl -fsSL https://deb.nodesource.com/setup_22.x -o nodesource_setup.sh
    

    Visit the official NodeSource distributions to verify the latest Node.js repository information packages.

  2. Run the script to install the repository and update your server's package information index.

    console
    $ sudo -E bash nodesource_setup.sh
    
  3. Install Node.js and NPM.

    console
    $ sudo apt-get install -y nodejs
    
  4. View the Node.js version.

    console
    $ node -v
    

    Your output should be similar to the one below.

    v22.5.1
  5. View the NPM version.

    console
    $ npm -v
    

    Output:

    10.8.2

Test Node.js

Follow the steps below to test your Node.js installation and run a sample HTTP server application that outputs a Greetings from Vultr message when you access it using a web browser.

  1. Create a new project directory. For example, nodeapp.

    console
    $ mkdir nodeapp
    
  2. Switch to the nodeapp directory.

    console
    $ cd nodeapp
    
  3. Create a new greetings.js file using a text editor like vim.

    console
    $ vim greetings.js
    
  4. Add the following contents to the greetings.js file.

    js
    import { createServer } from 'node:http';
    const server = createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Greetings from Vultr');
    });
    
    server.listen(3000, '127.0.0.1', () => {
      console.log('Listening on 127.0.0.1:3000');
    });
    

    Save and close the file.

    The above application code starts a new HTTP server that listens for incoming requests on port 3000 and displays a Greetings from Vultr message.

  5. Run the application using Node.js as a background process.

    console
    $ node greetings.js &
    

    Output:

    Server running at http://127.0.0.1:3000/
  6. Test access to the application using Curl on the host port 3000.

    console
    $ curl http://127.0.0.1:3000
    

    Output:

    Greetings from Vultr

Install Multiple Versions

NVM (Node Version Manager) is a command-line tool for installing, managing, and switching between different Node.js versions. Follow the steps below to install NVM and run multiple Node.js versions:

  1. Download the latest NVM installation script.

    console
    $ wget https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh
    
  2. Run the script to install NVM.

    console
    $ bash install.sh
    

    Output:

    => Downloading nvm as script to '/root/.nvm'
    => Appending nvm source string to /root/.bashrc
    => Appending bash_completion source string to /root/.bashrc
    => Close and reopen your terminal to start using nvm or run the following to use it now:
    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  3. Apply the NVM changes to your active shell environment.

    console
    $ source ~/.bashrc
    
  4. List all Node.js versions available for download.

    console
    $ nvm ls-remote
    

    Output:

    v0.1.14
    ...
    v20.15.1   (Latest LTS: Iron)
    ...
    v22.0.0
    ...
    v22.5.0
    v22.5.1
  5. Install a specific Node.js version and NPM on your server. For example, Node.js version 22.5.1.

    console
    $ nvm install 22.5.1
    

    Output:

    Downloading and installing node v22.5.1...
    Now using node v22.5.1 (npm v10.8.2)
    Creating default alias: default -> 22.5.1 (-> v22.5.1)
  6. Install another Node.js version, such as 20.15.1.

    console
    $ nvm install 20.15.1
    
  7. Switch to a specific Node.js version. For instance, 22.5.1.

    console
    $ nvm use 22.5.1
    

    Output:

    Now using node v22.5.1 (npm v10.8.2)
  8. Install the latest Node.js LTS version on your server.

    console
    $ nvm install --lts
    
  9. Install a specific LTS version such as lts/iron.

    console
    $ nvm install --lts/iron
    
  10. View the active Node.js version.

    console
    $ node -v
    

    Output:

    v20.15.1
  11. View the active NPM version.

    console
    $ npm -v
    

    Output:

    10.7.0

Conclusion

You have installed Node.js and NPM on Debian 12. You installed multiple versions to enable the development of web applications that require a specific version. Node.js lets you to develop and deliver web applications with all necessary dependency packages using the Node Package Manager (NPM). For more information, visit the Node.js documentation.