How to use npm package: axios in Laravel 5.5?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Use npm Package: Axios in Laravel 5.5

Integrating modern JavaScript libraries like Axios into a traditional Laravel setup, especially when dealing with frontend frameworks like Vue.js, requires careful handling of asset loading and server-side security. Many developers run into issues when trying to bridge the gap between Node package management (npm) and browser execution.

The core challenge you are facing—how to use axios in your Blade/Vue setup—is less about Laravel itself and more about correctly bundling and exposing JavaScript dependencies to the browser, especially within a framework context. As a senior developer, I can assure you that while the concept of using Axios for AJAX requests is straightforward, the implementation details depend heavily on how your assets are compiled and loaded.

Understanding the Role of Axios in Laravel Stacks

Axios is a promise-based HTTP client, making asynchronous requests clean and readable. When working with Laravel, the backend's primary role (as shown in your routes) is to serve data via APIs. The frontend's role is to consume that API using tools like Axios.

Your setup correctly demonstrates the necessary components:

  1. Laravel Backend: Defines the endpoint (/links) and provides the JSON response.
  2. Frontend (Vue/Axios): Initiates the request to fetch data from that endpoint.
  3. Security: Laravel's built-in CSRF protection must be respected for all cross-origin requests.

The Pitfall of Module Loading in Blade

You attempted to use require('axios') within a script block, which is common in Node environments (like testing or server-side code), but it doesn't directly translate to how browser JavaScript loads external libraries unless you are using a module bundler like Webpack or Vite. The reason your attempt likely failed is that the browser doesn't natively understand the require() syntax for loading NPM packages directly within a standard <script> tag without a build step.

For Laravel projects, especially older versions like 5.5, the most robust and simplest method to include Axios in your frontend assets is via a Content Delivery Network (CDN) link or by ensuring your asset pipeline (Mix/Vite) correctly bundles the dependency.

The Correct Implementation: Loading Axios Properly

The best practice for including client-side libraries like Axios in a Laravel project involves leveraging either CDN links or using your build system to manage dependencies.

Method 1: Using a CDN (Simplest Approach)

For quick setups, loading Axios via a CDN is the easiest way to get started, as it bypasses local bundling headaches. This method requires you to ensure that the necessary Laravel security headers are configured correctly on the server side.

In your main layout file (layouts/app.blade.php) or directly in your script tag, you would include Axios:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

Method 2: Integrated Loading with Laravel Security (The Best Practice)

As demonstrated in your bootstrap.js, you were correctly trying to handle the CSRF token, which is vital for security when making requests from a Laravel application. You managed this by reading the meta tag:

let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
    window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
}

This pattern is excellent because it ensures that every request made via Axios automatically includes the necessary CSRF header, adhering to Laravel's security protocols. This approach keeps your code clean and secure, which aligns perfectly with the principles of secure development promoted by the Laravel team on their documentation.

Finalizing Your Integration

When integrating this into your Vue setup (axisa.js), you simply use the globally available axios object that you have loaded:

// axisa.js (Revised)
new Vue({
    el: '#app',
    data: {
        skills: []
    },
    mounted(){
        // Now, 'axios' is globally available from the CDN or bundled asset
        axios({
            method: 'get',
            url: '/links' // This hits your Laravel route
        })
        .then(response => {
            this.skills = response.data;
        })
        .catch(error => {
            console.error("Error fetching data:", error);
        });
    }
});

Conclusion

Using npm packages like Axios in a Laravel project is entirely achievable. The key mistake often lies in assuming the npm install command automatically resolves frontend bundling issues. Instead, focus on correctly loading the library into the browser (via CDN or asset compilation) and ensuring your application respects the security measures provided by the framework, such as handling the CSRF token as you successfully did. By combining proper asset loading with Laravel's backend structure, you create robust and secure full-stack applications.