How to solve require is not defined?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Solving the `require is not defined` Error in Laravel/Vue Applications
As developers building modern, component-based applications using frameworks like Laravel and Vue.js, managing asset loading and module dependencies can often introduce tricky runtime errors. The error you are encountering, `require is not defined`, is a classic symptom that arises when attempting to use Node.js-style CommonJS module loading (`require()`) in an environment (like a standard browser script) where that function has not been properly initialized or loaded by the preceding system.
This post will dive deep into why this happens, analyze your specific setup using RequireJS, and provide robust solutions to ensure your front-end dependencies load correctly within your Laravel ecosystem.
## Understanding the Root Cause: Module Loading Context
The `require` function is not a native JavaScript feature available globally in all browser environments; it is typically provided by module loaders like RequireJS or bundlers like Webpack/Vite when processing code for the browser.
When you see `require is not defined`, it means that the line of code attempting to execute `require(...)` is running *before* the module loader has successfully initialized and exposed the `require` function into the global scope, or before the necessary modules have been loaded by the loader itself.
In your case, even though you included the RequireJS script, the way you are structuring the dependency loading within `app.js` seems to be fighting against how RequireJS manages module paths and execution order.
## Analyzing Your RequireJS Implementation
Let's look at the code structure you provided:
```javascript
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app'
}
});
requirejs(['jquery', 'canvas', 'app/sub'],
function ($, canvas, sub) {
});
// this line below is causing the problem
require('./bootstrap'); // <-- Error likely happens here or later
window.Vue = require('vue');
Vue.use(require('vue-chat-scroll'));
// ... other requires
```
The issue often lies in mixing direct script loading with module loading calls, especially when referencing local files using relative paths like `./bootstrap`. RequireJS expects all dependencies to be resolved through its configuration and dependency graph.
## The Solution: Adhering to Module Loading Principles
To fix this, we need to ensure that every component, library, and bootstrap script is loaded explicitly as a module dependency within the RequireJS framework, rather than relying on implicit execution order.
### 1. Correct Initialization Order
Ensure that all necessary modules are listed in the initial `requirejs` call. If you have a file like `./bootstrap.js`, it must be included when initializing the loader or explicitly requested as a dependency.
A cleaner approach is to define *all* required libraries upfront:
```javascript
requirejs.config({
baseUrl: 'js/lib',
paths: {
app: '../app'
}
});
// Load all primary dependencies in one go
requirejs.config({
paths: {
// ... configuration remains the same
}
});
// Requesting modules explicitly resolves the dependency chain
requirejs.load({
'jquery': 'jquery', // Assuming jquery is in js/lib/jquery.js
'canvas': 'canvas', // Assuming canvas is in js/lib/canvas.js
'app/sub': 'app/sub',
'vue': 'vue', // Load Vue as a module
'vue-chat-scroll': 'vue-chat-scroll', // Load the scroll extension
'./bootstrap': './bootstrap' // Ensure your bootstrap file is loaded correctly
});
// After loading, you can access modules via the RequireJS object itself,
// or ensure they are injected into the global scope if necessary.
```
### 2. Handling Vue Dependencies
When dealing with Vue components and plugins, treating them as explicit modules is crucial. For instance, instead of mixing `window.Vue = require('vue')` and `Vue.use(require('vue-chat-scroll'))`, you should let RequireJS manage the injection process entirely.
If you are using Laravel, remember that clean separation of concerns is key to maintainable code, much like adhering to the principles taught by Laravelâkeeping your framework logic separate from your presentation layer. When structuring assets, think about how the server (Laravel) passes configuration versus what the client (browser) executes.
## Conclusion
The `require is not defined` error in a RequireJS setup is almost always an issue of dependency resolution and execution timing rather than a flaw in the module itself. By strictly adhering to the structure of the module loaderâdefining all dependencies explicitly within the `requirejs.load()` or initial loading callsâyou ensure that the JavaScript environment correctly sets up the necessary context before attempting to call any specific module functions. By treating your front-end assets with the same rigor you apply to backend architecture, you can eliminate these frustrating runtime errors and build more stable applications.