Last modified: Jul 16, 2025 By Alexander Williams

Install Axios Module in Node.js

Axios is a popular HTTP client for Node.js. It simplifies making HTTP requests. This guide will help you install and use it.

Prerequisites

Before installing Axios, ensure you have Node.js installed. You can check by running:


node -v

If Node.js is not installed, download it from the official website. Also, ensure npm (Node Package Manager) is working.

Install Axios Using npm

To install Axios, open your terminal and navigate to your project folder. Run the following command:


npm install axios

This will download and install Axios in your project. The module will be added to the node_modules folder.

Install Axios Using Yarn

If you prefer Yarn, use this command instead:


yarn add axios

Yarn is an alternative to npm. Both work well for managing Node.js packages.

Verify Installation

After installation, verify Axios is installed correctly. Create a test file and import Axios:


const axios = require('axios');
console.log('Axios installed successfully!');

Run the file using Node.js:


node test.js

If no errors appear, Axios is installed correctly. If you see an error, check our guide on how to fix missing modules.

Basic Usage of Axios

Axios makes HTTP requests easy. Here’s a simple GET request example:


axios.get('https://jsonplaceholder.typicode.com/posts/1')
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error('Error:', error);
  });

This code fetches data from a test API. The response is logged to the console.

Common Errors and Fixes

Sometimes, you may encounter errors. One common issue is Cannot find module 'axios'. This happens if Axios is not installed.

To fix it, reinstall Axios using npm or Yarn. If the issue persists, check our guide on fixing missing modules.

Global vs Local Installation

Axios can be installed globally or locally. For most projects, a local installation is better. It avoids version conflicts.

To install Axios globally, use:


npm install -g axios

However, global installations are rarely needed. Stick to local installations for better project management.

Using Axios with Express

Axios works well with Express. If you need help setting up Express, check our guide on installing Express.

Here’s an example of using Axios in an Express app:


const express = require('express');
const axios = require('axios');
const app = express();

app.get('/data', async (req, res) => {
  try {
    const response = await axios.get('https://api.example.com/data');
    res.json(response.data);
  } catch (error) {
    res.status(500).json({ error: 'Failed to fetch data' });
  }
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

This code creates an Express server that fetches data using Axios.

Conclusion

Installing Axios in Node.js is simple. Use npm or Yarn to add it to your project. Verify the installation and start making HTTP requests.

If you face issues, check the error messages. Reinstall Axios if needed. For more help, refer to our troubleshooting guides.

Axios is a powerful tool for handling HTTP requests. It simplifies working with APIs in Node.js.