Sign In
Sign In

How to Create a Bot in Discord

How to Create a Bot in Discord
Hostman Team
Technical writer
Node.js
16.05.2024
Reading time: 10 min

Bots are an integral part of server management. They significantly save time and allow you to efficiently solve trivial problems. There are ready-made solutions, but they are limited in functionality and are not always suitable. In this case, you can create a bot yourself. Let's look at how to create a bot in Discord that will allow you to implement all the necessary functionality.

There are several ways to create a Discord bot. To do this, you can use special software, write code in Python or discord.js. 

We will describe creating a bot in JS and adding it to the server. 

Development environment

At the initial stage, you need to select and install a development environment. We will use Visual Studio Code as an example, but you can also use other development environments, such as Atom, Eclipse, Notepad ++, and others. Having selected the environment, download it from the official website for your operating system version.

In addition to the development environment, you need to install the necessary extensions, as well as the runtime environment. The runtime environment is node.js, and extensions can be installed directly from the development environment window.

What extensions can be useful when creating a Discord bot in JS? First of all, these are tools for running selected fragments of the generated code, as well as an extension for conveniently displaying the workspace. If you use Visual Studio Code, you can find these extensions under the names Code Runner and Discord Presence.

Creating the bot

The process of creating a Discord bot begins in the developer portal. To do this, select the NEW APPLICATION option.

Image5

In the dialog box, assign a name for the new bot and click CREATE. You can easily change the bot's name while working and also add an icon.

Image4

The last step is to add the required permissions. Go to the BOT tab, and at the bottom of the page, set the permissions the bot should have. Checking the Administrator checkbox will select everything.

Image7

Before writing code, create a folder and open it in Visual Studio Code. To do this, use file -> open folder.

The next step is to create a terminal through which we will add a description of the bot. To do this, select: terminal -> create terminal.

To start creating a description of the bot in the terminal, enter:

npm init

Press Enter at the end of each line. Next, enter the following combinations:

npm install
npm install discord.js

After this, you should see a folder with files and two objects with the .json extension.

Writing code

First, create a config.json file to store basic data about the bot. In the file indicate the following:

{
"token": "your_token",
"prefix": "your_prefix"
}

The token can be obtained on the developer portal in the Bot tab. This information is displayed once, so be sure to save your token.

Image3

The starting point of the command is used as a prefix. For example, if the command $help is entered, then the $ sign will be the prefix. Usually the exclamation mark ! is used as a prefix. However, you can use almost any character as a prefix, including letters, numbers, and special characters, as long as they are not reserved or otherwise used in your bot.

The next step is to create the bot.js file, which is the body of our bot and contains its functionality, which can later be expanded and edited. A typical file looks like this:

const Discord = require('discord.js'); // Retrieving the discord.js library
const robot = new Discord.Client({ intents: [
     Discord.GatewayIntentBits.Guilds,
     Discord.GatewayIntentBits.GuildMessages,
     Discord.GatewayIntentBits.MessageContent //This directive is required for the bot to respond to the message body
]}) // Declaring that robot is a bot. This part of the code contains data that depends on the version of the discord.js library; in the example, Discord.js v14 is used
const comms = require("./comms.js"); // Retrieving a file with commands for the bot
const fs = require('fs'); // Retrieving the native node.js file system module
let config = require('./config.json'); // Retrieving a file with parameters and information
let token = config.token; //Retrieving the token from it
let prefix = config.prefix; //Retrieving the prefix from it
robot.on("ready", function() {
     /* Upon successful launch, the console will display the message “[Bot name] has launched!” */
     console.log(robot.user.username + "started!");
});
robot.on('messageCreate', (msg) => { // Respond to messages
     if (msg.author.username != robot.user.username && msg.author.discriminator != robot.user.discriminator) {
         var comm = msg.content.trim() + " ";
         var comm_name = comm.slice(0, comm.indexOf(" "));
         var messArr = comm.split(" ");
         for (comm_count in comms.comms) {
             var comm2 = prefix + comms.comms[comm_count].name;
             if (comm2 == comm_name) {
                 comms.comms[comm_count].out(robot, msg, messArr);
}
}
}
});
robot.login(token); // Bot authorization

Please note that the config.json file must be placed in the same directory as your bot.js so that the bot can read the settings from the file correctly.

To work with the bot parameters, create a file in which the commands will be described. We name the file comms.js and add the code to it:

const config = require('./config.json'); // Retrieving a file with parameters and information
const Discord = require('discord.js'); // Retrieving the discord.js library
const prefix = config.prefix; //"Pull out" the prefix
// Commands //
function test(robot, mess, args) {
mess.channel.send('Test!')
}
// List of commands //
var comms_list = [{
     name: "test",
     out: test,
     about: "Test command"
}];
// Name is the name of the command to which the bot will respond
// Out is the function with the command
// About is the description of the command
module.exports.comms = comms_list;

This is an example, if necessary, you can add more functions and commands.

Before launching the bot

Important! Latest versions of Discord require the latest versions of Node.js. For example, Discord version 13 requires at least Node 16 to run. By default, the system uses the old Node 14 version.

You can check the version with the command:

node -v

You can update the version with the commands:

For Ubuntu:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

For Debian:

curl -fsSL https://deb.nodesource.com/setup_20.x | bash - &&\
apt-get install -y nodejs

The error Unexpected token '??=' indicates an incorrect version; you will need Node 16+.

Launching the bot

Now let's run the bot in the terminal.

node bot.js

After launch, you can select privileges. To do this you need:

  1. Go to the OAuth2 tab.

  2. In the Scopes block, check the box next to bot.

Image6

  1. In the Bot permissions block that appears, check the required privileges. Checking the Administrator checkbox selects everything.

  2. Below in the same tab, you can get a link to the bot. Just copy it and paste it into the address bar of your browser.

Image2

  1. Also enable the Message Content Intent option on the Bot tab:

34a5ed5f 34dc 4604 97b8 3f49a144accf

How to create a server and connect a bot

For the bot you need to create a channel in Discord.

  1. Log into your Discord account in a browser.

  2. To add a server, click the "+" icon in the sidebar and indicate which server template you want to use and for what purposes you will use it.

  3. In the next step, assign a server name and image.

  4. Click Create.

By default, the new server has voice and text channels. Each of them has settings that can be changed by clicking on the gear icon. You can add more channels by clicking the "+" icon. To assign an ID to a channel, you need to switch to developer mode: app settings -> advanced -> developer mode.

To connect a bot, you need to get a link in OAuth2 -> URL Generator.

Image1

As we indicated above, in Scopes check the bot box, below in Bot permissions select permissions (the Administrator selects everything).

Paste the link to the bot into the address bar.

Once you open the application, you can select a server from the drop-down list. Check that all necessary permissions for the bot are assigned. If everything is done correctly, when you return to the server, you will see a welcome message.

How to edit a bot

You can update your bot, add new options, and make the bot more user-friendly, functional, and personalized.

To make the bot encourage users, you can add a list of key phrases in response to which welcome or other messages will be displayed. To add new functionality (trigger phrase) for the bot, you need to enter data into the comms.js file. For example, we will modify the test function. An example of adding and editing trigger phrases looks like this:

const config = require('./config.json'); // Retrieving a file with parameters and information
const Discord = require('discord.js'); // Retrieving the discord.js library
const prefix = config.prefix; //Retrieving the prefix
const sad_words = ["sad", "depressed", "unhappy", "angry", "miserable"]; 
const starter_encouragements = ["Cheer up!", "Hang in there.", "You are a great person / bot!"];
// Words can be specified in any language
// Commands //
function test(robot, mess, args) {
     if (sad_words.some(word => mess.content.includes(word))) {
         mess.channel.send("Why are you sad? Maybe I can help you?");
     } else {
         mess.channel.send('Test!');
     }
}
function encourage(robot, mess, args) {
    const encouragement = starter_encouragements[Math.floor(Math.random() * starter_encouragements.length)];
    mess.channel.send(encouragement);
}
// List of commands //
var comms_list = [{
     name: "test",
     out: test,
        about: "Edited test command"
    },
    {
        name: "encourage",
        out: encourage,
        about: "Encouraging a user"
    }
];
module.exports.comms = comms_list;

Now your Discord bot will respond to messages containing trigger phrases/words from the sad_words list and send an appropriate response.

You can check the changes by sending a message like:

!test sad

With this directive, we add phrases with which the bot will respond to trigger words:

starter_encouragements = ["Cheer up!", "Hang in there.", "You are a great person / bot!"]

We added a new function called encourage, which will select a random phrase from the starter_encouragements list and send it to the channel. We then added this function to the comms_list, specifying the command name, the encourage function, and its description.

You will also need to make changes to the bot.js file (if the functionality was not included initially) so that it recognizes the new encourage command. Make sure you have the following code in your bot.js file:

for (comm_count in comms.comms) {
     var comm2 = prefix + comms.comms[comm_count].name;
     if (comm2 == comm_name) {
         comms.comms[comm_count].out(robot, msg, messArr);
     }
}

You can now use the !encourage command in Discord chat and the bot will send a random phrase of encouragement from the starter_encouragements list in response.

What is Discord Bot Maker

Discord Bot Maker is another option for creating a bot in Discord. Using the tool, you can not only create standard solutions, but also develop bots that send files, generate messages, and edit pictures.

To get started, you need to install the utility and click the Create button. After this, you can select the required set of functions and commands. Don't forget to save the bot, after that you can launch it on Discord.

Conclusion

We've covered the basic steps of creating a Discord bot, a channel, adding it to the server, and the basic commands that can be added if necessary. The process is not easy, it has many nuances, but you can figure them out and create your own personalized Discord bot that meets your goals.

Node.js
16.05.2024
Reading time: 10 min

Similar

Node.js

How to Handle Asynchronous Tasks with Node.js and BullMQ

Handling asynchronous tasks efficiently is crucial in Node.js applications, especially when dealing with time-intensive operations like sending emails, processing images, or performing complex calculations. Without proper management, these tasks can block the event loop, leading to poor performance and a subpar user experience. This is where BullMQ comes into play. BullMQ is a powerful Node.js package that offers a reliable and scalable queuing system powered by Redis. It enables developers to transfer heavy operations to a queue in the background, keeping the main application responsive. With BullMQ you can successfully manage async queues, plan processes, and easily keep an eye on their progress. This tutorial will show you how to manage asynchronous tasks with Node.js and BullMQ. The process involves setting up a project folder, performing a time-intensive task without using BullMQ, and enhancing the application by incorporating BullMQ for running tasks in parallel. Prerequisites Before you begin, ensure you: Set up a Linux server. Set up Node.js on your server. Set up Redis on your server, as BullMQ depends on Redis for managing queues. Setting Up the Project Directory Before you can use Node.js and BullMQ for asynchronous tasks, it is necessary to establish your project directory. Set up and customize your Node.js application using these guidelines. Create a New Directory Open your terminal and go to the location of your project. Create a fresh folder and navigate into it: mkdir bullmq-demo && cd bullmq-demo Initialize a New Node.js Project Set up a Node.js project using npm. It generates a package.json file containing the default configurations: npm init -y Install Required Dependencies Set up the required packages for your application: npm install express bullmq ioredis Here's what each package does: express: A fast Node.js web framework commonly used for server creation. bullmq: An excellent tool for handling queues within Node.js programs. ioredis: A Redis client for Node.js that BullMQ needs in order to establish a connection with Redis. Create the Main Application File Create an index.js file as the primary access point for your application: touch index.js Alternatively, you have the option to generate this file by using your code editor. Set Up a Basic Express Server To set up a simple Express server, include this code in your index.js file: const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); This code initiates an Express app on port 3000 which handles requests using JSON middleware. Verify the Server Setup Start the server by running: node index.js The below message should appear: Open up your internet browser and go to either http://your_server_ip:3000 or http://localhost:3000. You will receive a "Cannot GET /" message as there are no routes set up, as anticipated. When ready to proceed, you can terminate the server using Ctrl + C. Implementing a Time-Intensive Task Without BullMQ This part describes how to include a route in your Express app that performs a time-consuming task in a synchronous way. This will demonstrate how specific tasks can block the event loop and negatively affect your application's performance. Define a Time-Intensive Function Create a function in the index.js file that simulates a computationally intensive task: // Function to simulate a heavy computation function heavyComputation() { const start = Date.now(); // Run a loop for 5 seconds while (Date.now() - start < 5000) { // Perform a CPU-intensive task Math.sqrt(Math.random()); } } The function runs a loop for about five seconds, performing math operations to mimic a CPU-heavy task. Create a Route to Handle the Task Create a fresh route in your Express application that calls the heavyComputation function: app.get('/heavy-task', (req, res) => { heavyComputation(); res.send('Heavy computation finished'); }); This route is set up to receive GET requests specifically at the /heavy-task endpoint. After receiving a request, it carries out the specified intensive computation and then provides a response. Start the Server To restart your server, execute the following command: node index.js Confirm the server is functioning before moving on to the next stage. Test the Heavy Task Route Open your internet browser and type in either http://your_server_ip:3000/heavy-task or http://localhost:3000/heavy-task to access the webpage.  The following message should be displayed: It is important to observe that the response time is approximately five seconds. The delay is a result of the synchronous execution of the intensive computation process. Observe Blocking Behavior After the server is up and running, open a new tab on your internet browser and go to http://your_server_ip:3000/. The response to this request may not be immediate. The system delays taking action until the extensive processing of the previous step. This happens when the time-consuming task is blocking the Node.js event loop, stopping the server from processing additional incoming requests. When the server performs a task that takes a lot of time in a synchronous manner, it is unable to respond to additional requests. The act of blocking could result in a suboptimal user experience, particularly in apps that need to be highly responsive. Executing Time-Intensive Tasks Asynchronously with BullMQ We saw in the last section how synchronous execution of time-consuming operations can severely affect your application's performance by slowing down the event loop. This section explains how to implement a high-performance asynchronous queue into your application using BullMQ. Modify index.js to Use BullMQ Make changes to the index.js file to include BullMQ in your application. Import BullMQ and ioredis At the top of your index.js file, you should include the following import statements: const { Queue, Worker } = require('bullmq'); const Redis = require('ioredis'); Create a Redis Connection Next, set up a connection with Redis: const connection = new Redis(); Redis has been programmed to run on port 6379 and the localhost interface by default. To create a connection to a remote Redis server that has a different port, please enter the appropriate host address and port number: const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); Initialize a BullMQ Queue Create a new queue called heavyTaskQueue: const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); Add a Route to Enqueue Tasks Change the heavy-task route to add a job to the queue instead of running the task right away: app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); The application will respond after a lengthy process has completed, handling requests asynchronously, when the /heavy-task route is accessed. Remove the Worker Code from index.js The worker must be implemented in a separate file. This is essential to ensure that the worker does not coexist with the Express server process. A worker's use of the heavyComputation function during execution won't interfere with the event loop of the main application. The index.js file is structured in the following way: const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); const { Queue } = require('bullmq'); const Redis = require('ioredis'); const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); app.listen(port, () => { console.log(`Server is running on port ${port}`); }); Create a Separate Worker File Generate a fresh file and name it worker.js. The file is intended for executing the worker code in charge of handling tasks obtained from the queue. Create the worker.js file: touch worker.js Add Worker Code to worker.js: const { Worker } = require('bullmq'); const Redis = require('ioredis'); const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); // Function to simulate a heavy computation function heavyComputation() { const start = Date.now(); // Run a loop for 5 seconds while (Date.now() - start < 5000) { // Perform a CPU-intensive task Math.sqrt(Math.random()); } } const worker = new Worker( 'heavyTaskQueue', async job => { // Time-intensive task here heavyComputation(); console.log('Heavy computation completed'); }, { connection } ); worker.on('completed', job => { console.log(`Job ${job.id} has completed`); }); worker.on('failed', (job, err) => { console.log(`Job ${job.id} has failed with error ${err.message}`); }); Run the Worker in a Separate Process You must now execute worker.js as an independent Node.js process. Start the Worker Process Open a new terminal window or tab, navigate to your project folder, and run the specified command: node worker.js Start the Express Server Initiate the Express server in your original terminal window: node index.js Test the Application with BullMQ Proceed to conduct testing of the application utilizing BullMQ.  Make a Request to /heavy-task:Open your internet browser and type in either http://your_server_ip:3000/heavy-task or http://localhost:3000/heavy-task in the URL bar. The following message should be displayed: Heavy computation job added to the queue. The rapid response time suggests that there is no blockage in the main thread. Adding a Dashboard to Monitor BullMQ Queues Monitoring your application's queues and jobs is essential for ensuring they are functioning properly and for troubleshooting purposes. BullMQ comes with a functionality called Bull Board, which offers a visual interface for overseeing your queues. This part explains how to incorporate a dashboard into your application. Install Bull Board Use npm to install the @bull-board/express package: npm install @bull-board/express Set Up Bull Board in Your Application In order to set up the bull board application, follow these steps: Import Bull Board Modules Insert the code provided at the top of your index.js file: const { createBullBoard } = require('@bull-board/api'); const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); const { ExpressAdapter } = require('@bull-board/express'); Create an Express Adapter for the Dashboard Initialize the Express adapter: const serverAdapter = new ExpressAdapter(); serverAdapter.setBasePath('/admin/queues'); Set Up Bull Board with Your Queue Create the Bull Board instance and pass your queue: createBullBoard({ queues: [new BullMQAdapter(heavyTaskQueue)], serverAdapter: serverAdapter, }); Use the Dashboard in Your Express App Add the following line to mount the dashboard at /admin/queues: app.use('/admin/queues', serverAdapter.getRouter()); Make sure to include this line following the setup of your queue and worker. The final index.js file looks like below: // Import Express and Initialize App const express = require('express'); const app = express(); const port = 3000; app.use(express.json()); // Import BullMQ and Redis const { Queue } = require('bullmq'); const Redis = require('ioredis'); // Redis Connection const connection = new Redis({ host: '127.0.0.1', port: 6379, maxRetriesPerRequest: null, }); // Initialize Queue const heavyTaskQueue = new Queue('heavyTaskQueue', { connection }); // Define Route to Add Job to Queue app.get('/heavy-task', async (req, res) => { await heavyTaskQueue.add('heavyComputation', {}); res.send('Heavy computation job added to the queue'); }); // Import Bull Board and Set Up Dashboard const { createBullBoard } = require('@bull-board/api'); const { BullMQAdapter } = require('@bull-board/api/bullMQAdapter'); const { ExpressAdapter } = require('@bull-board/express'); const serverAdapter = new ExpressAdapter(); serverAdapter.setBasePath('/admin/queues'); createBullBoard({ queues: [new BullMQAdapter(heavyTaskQueue)], serverAdapter: serverAdapter, }); app.use('/admin/queues', serverAdapter.getRouter()); // Start the Server app.listen(port, () => { console.log(`Server is running on port ${port}`); }); Access the Dashboard To access the dashboard, follow the steps listed below: Restart Your Server node index.js Navigate to the Dashboard Open your browser and go to http://your_server_ip:3000/admin/queues. Explore the Dashboard: Queue Overview: See the list of queues and their status. Jobs List: View active, completed, failed, and delayed jobs. Job Details: Click on a job to see its data, logs, and stack trace if it failed. You can easily manage your BullMQ queues by integrating Bull Board into your application. It is much easier to keep an eye on progress and identify issues when you can view your queues and tasks on the dashboard in real-time. Conclusion You have now learned how to use BullMQ with Node.js to manage asynchronous processes. Your application's responsiveness and efficiency have been enhanced by moving time-consuming operations to a separate queue. Your Node.js app is now much more capable of handling heavy demands thanks to the usage of queues.
28 November 2024 · 11 min to read
Node.js

How to Install Node.js and NPM on Ubuntu 24.04

The popular JavaScript runtime Node.js enables server-side programming with JavaScript. NPM, a package manager for Node.js projects, helps with dependency management. This guide will show how to install NPM and Node.js on Ubuntu 24.04. Prerequisites System running in Ubuntu 24.04 Root access or user with sudo privileges Installing Node.js and npm from the Default Ubuntu Repository Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below:  sudo apt update && sudo apt upgrade -y Node.js is normally available from Ubuntu's default repository. Install it by running the following command: sudo apt install nodejs npm  -y Installing Node.js and npm via the NodeSource Repository Add the NodeSource repository for Node.js:  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -  Replace setup_20.x with the desired version. Different version can be found on nodesource.com. Use the following command to install Node.js after adding the NodeSource repository: sudo apt install nodejs -y Verifying the Node.js and npm Installation Verify the following versions of Node.js and npm to make sure they were installed correctly. Run the below command. node -v npm version Installing Specific Node.js Versions with NVM  With the help of the robust utility Node Version Manager (NVM), devops may easily manage several Node.js versions on a single machine. This is very helpful when switching between several project needs. To install NVM, download and run the installation script from the NVM repository using the following command: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash After running the scripts, source the user profile and add NVM to the shell session. Add the following lines to the user's home directory (~/.bashrc, ~/.zshrc, or the corresponding shell profile script). Create it using nano editor: nano ~/.bashrc 3. Add the following content: export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" Run the command below so changes will take effect. source ~/.bashrc With NVM installed, install the specific versions of Node.js. In this case, to install Node.js version 16, run the command below: nvm install 16 Switch to a specific version of Node.js that is installed, using the command below. nvm use 16 Managing Node.js Projects Several essential procedures and best practices are involved in managing Node.js projects in order to ensure the effectiveness, maintainability, and scalability of the application. This is a tutorial to help to efficiently manage your Node.js projects. Launch the terminal, navigate to the project creation path, and make a folder named after the project you are creating. mkdir my_project Initiate the Node project by running the command npm init. Provide the required details (marked in red). All of the answers in this example will be default. The file package.json will result from this. npm init Install any required dependencies, such as nodemon and express. The package-lock.json file and the node_modules folder will be created as a result. npm i express nodemon To initialize git in the project, use the git init command. This will include the file .gitignore. git init Make a file called Readme.md that will have all of the project's information. touch Readme.md Make a file with the .env extension that will hold the project's credentials and sensitive data. touch process.env To launch the program, create a file with the name app.js or index.js. touch app.js Make two folders: Public (which contains resources and static files) and src (which contains controllers, models, routes, and views). mkdir Public src Check each and every folder and file that was generated. This is how a typical structure might look like. For the NODE JS application, it is best practice to establish a project structure, divide files based on their function, and place those files in the appropriate directories. To make it simple to confirm the existence and logic of any given file or folder, unify the application's naming conventions and include business logic in the controllers folder, for instance. ls -lrt Best Practices for Node JS Project Structure For production systems, set up logging and monitoring with tools like Datadog or New Relic. Plan routine maintenance activities including performance reviews, security audits, and dependency updates. Put in place a backup plan for important configurations and data. Check for security flaws in your dependencies and code on a regular basis. Troubleshooting Common Issues There are some frequent problems that a user could run into when installing npm and Node.js. These troubleshooting instructions should help you to address the majority of typical problems that arise when installing npm and Node.js. The steps for troubleshooting these issues are listed below: When attempting to install Node.js or npm globally (i.e., using sudo), users get permission-related issues that prevent them from finishing the installation process. After installing nvm, the command is not recognized. The error nvm Command Not Found will be encountered. Make sure that the shell's configuration file (.bashrc, .bash_profile, .zshrc, etc.) has nvm sourced, and then the command source ~/.bashrc has been use to reload it. The npm version is out of date or does not correspond with the Node.js version after installing Node.js. Use nvm install <version> to install a particular Node.js version, which will include the matching npm version, and manually update npm by running npm install -g npm.  Conclusion In conclusion, an important initial step in creating new web applications and utilizing server-side JavaScript is installing Node.js and npm. Although installing software is usually simple, there are a few frequent problems that can arise, such as permissions conflicts, environment setup problems, or version mismatches. One can successfully overcome these problems by configuring npm to be compatible with your network environment, modifying system settings for global installations, and managing Node.js versions with tools like nvm. Do not forget to update npm and Node.js frequently to take advantage of the newest features and security updates. It will have a strong base for developing and implementing Node.js-powered, scalable applications with correct setup and troubleshooting.
09 September 2024 · 6 min to read
Node.js

Installing and Using NVM (Node Version Manager)

Node Version Manager (NVM) is a powerful tool designed to manage multiple versions of Node.js on a single machine. It simplifies the process of installing, updating, and switching between different versions of Node.js, making it an essential tool for developers working on various projects with different Node.js requirements. Benefits of Using NVM Version Management: Easily install and switch between different versions of Node.js. Environment Isolation: Maintain separate Node.js versions for different projects. Convenience: Simplifies testing and development by providing quick version switching. Installing NVM To install NVM, follow these steps: 1. Download and Install Script Open a terminal and run the following command to download and install NVM: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash Output: 2. Load NVM After the installation, you need to add NVM to your shell profile. Run the following command: export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"   [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" 3. Verify Installation Ensure that NVM is installed correctly by checking its version: nvm --version Output: Installing Node.js Versions with NVM With NVM installed, you can now install different versions of Node.js. 1. Install a Specific Version To install a specific version of Node.js, use the following command: nvm install <version> Replace <version> with the desired Node.js version number (e.g., 14.17.0). Output: 2. Install the Latest LTS Version To install the latest Long-Term Support (LTS) version of Node.js, run: nvm install --lts Output: Switching Between Node.js Versions NVM allows you to switch between installed Node.js versions easily. 1. List Installed Versions To view all installed Node.js versions, use: nvm ls Output: 2. Switch to a Specific Version To switch to a specific version, run: nvm use <version> Replace <version> with the version number you want to use. Setting a Default Node.js Version You can set a default Node.js version that will be used in all new shell sessions. Set Default Version To set a default Node.js version, use: nvm alias default <version> Replace <version> with the desired Node.js version number. Uninstalling Node.js Versions with NVM NVM also provides an easy way to uninstall Node.js versions that are no longer needed. Uninstall a Specific Version To uninstall a specific Node.js version, run: nvm uninstall <version> Replace <version> with the version number you want to uninstall. Conclusion Using NVM (Node Version Manager) simplifies the management of multiple Node.js versions, providing a convenient and efficient workflow for developers. By following the steps outlined in this tutorial, you can easily install, switch, and manage different Node.js versions on your system. This tutorial provides a comprehensive guide to installing and using NVM, ensuring a smooth and efficient Node.js version management experience.
17 July 2024 · 3 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support