How do I add popper.js to a Laravel 8 with Bootstrap 5 project?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Seamlessly Add Popper.js to Your Laravel 8/Bootstrap 5 Project

Building modern front-end applications on a Laravel foundation, especially with frameworks like Bootstrap and asset pipelines like Laravel Mix, often involves managing external dependencies. When you run into issues integrating popular libraries like Popper.js, it usually points to a mismatch between how the dependency is installed via npm and how your JavaScript bundler (Webpack/Mix) resolves those modules.

This post will walk you through diagnosing why your attempt to include Popper.js failed during compilation and provide the correct, robust method for integrating external dependencies into your Laravel project structure.

Understanding the Setup Context

You have a standard setup:

  1. Backend: Laravel 8.
  2. Frontend Style: Bootstrap 5.
  3. Asset Compilation: Laravel Mix (Webpack).
  4. JS Structure: Dependencies reside in resources/js.
  5. Goal: Integrate Popper.js for proper positioning within your Bootstrap modals or tooltips, requiring it to be bundled correctly.

Your current approach involves using require('./popper') in app.js, expecting Webpack to handle the inclusion of the necessary files from node_modules. The failure occurs because while you install the package via npm, simply referencing a local file path within your JS requires specific configuration for the bundler to recognize it as an external module dependency.

The Correct Approach: Leveraging NPM Dependencies in Webpack

The most reliable way to integrate external JavaScript libraries into a Laravel Mix project is to treat them strictly as Node modules. Instead of trying to import files directly from the local resources/js directory, you should leverage the contents of your node_modules folder.

Step 1: Correct Installation and Dependency Management

First, ensure that the dependency is installed correctly at the root of your project:

npm install @popperjs/core --save
# or using yarn:
# yarn add @popperjs/core

This action places the necessary files into your node_modules directory. Now, we must adjust how you import it in your JavaScript files.

Step 2: Modifying the Import Statement

Instead of relying on relative paths (require('./popper')), which can be ambiguous for Webpack when dealing with external NPM packages, you should import the library directly using its module path. This allows Webpack to correctly resolve the dependency tree from node_modules.

In your primary entry file, such as resources/js/app.js, modify your imports:

Before (Problematic):

require('./bootstrap');
require('./popper'); // Fails to resolve correctly for external packages

After (Corrected):
You should import Popper directly where it is needed, ensuring that the base CSS/JS files are handled by your standard entry points. For example, if you need Popper functions globally accessible, you import it like this:

// resources/js/app.js

// Import necessary external libraries explicitly
import Popper from '@popperjs/core'; 
import './bootstrap'; // Your custom bootstrap setup

// Now use Popper in your code...
// Example usage:
const popperInstance = new Popper(); 

By using the import syntax, you are telling Webpack exactly where to look for the module—in the node_modules directory—which is the intended mechanism for managing third-party libraries in a modern build environment like Laravel Mix. This aligns with the principles of modularity that underpin robust application development, much like the structured approach advocated by the Laravel ecosystem.

Finalizing the Build Configuration

Since you are using ES Modules (import/export), ensure your Webpack configuration is set up to handle these modules correctly within your webpack.mix.js. While Laravel Mix handles a lot automatically, ensuring your entry point starts with standard module syntax helps the bundling process recognize external dependencies properly.

If you encounter further issues related to asset compilation or dependency resolution, remember that understanding how assets are compiled is key. For more insights into structuring modern PHP and JavaScript applications, explore resources provided by the Laravel team at laravelcompany.com.

Conclusion

The issue of failing to compile Popper.js stems from a common pitfall: confusing file system referencing with module resolution in a bundler environment. By switching from local require('./popper') to explicit NPM import statements, you empower Webpack to correctly scan the node_modules folder and bundle the required code into your final public/js/app.js. This method ensures that your application remains modular, maintainable, and compatible with Laravel’s asset pipeline.