simple way to include node_modules in laravel 5.5
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
The Right Way to Handle node_modules in Laravel 5.5: Mastering Asset Bundling
As developers working with older frameworks like Laravel 5.5, one of the common stumbling blocks is integrating third-party npm packagesâthe contents of the node_modules directoryâinto the final application build. You correctly identified a common issue: installing packages like Moment.js and trying to reference them directly in your HTML scripts results in errors because the server environment (PHP/Blade) does not understand the Node dependency structure.
This post will dive into why direct inclusion fails, and more importantly, show you the correct, modern approach using asset management tools like Laravel Mix to properly integrate dependencies.
Why Direct Inclusion Fails
When you run npm install angular-moment moment --save, Node creates a node_modules directory containing the actual source code for those libraries. While this is essential for development, attempting to reference files directly via paths like <script src="node_modules/moment/moment.js"> fails for several reasons:
- Server Context: The web server (Apache/Nginx running PHP) cannot resolve these relative paths in the context of a browser request.
- Bundling Philosophy: Modern front-end development relies on bundlers (like Webpack, which Laravel Mix utilizes) to analyze dependencies, compile code, resolve module imports, and bundle everything into optimized, production-ready files. Simply linking static files bypasses this crucial optimization step.
The core philosophy of modern Laravel development, as advocated by the team at laravelcompany.com, is to leverage robust build pipelines rather than manually managing file paths in the view layer.
The Correct Approach: Leveraging Webpack for Dependency Management
Instead of trying to link individual modules, the solution lies in letting your asset pipeline handle the dependency resolution. When you use a bundler, the process changes from "include this file" to "import this module," allowing the bundler to manage the entire dependency tree.
Step 1: Import Dependencies within JavaScript
If you are using Webpack (via Laravel Mix), ensure that your entry JavaScript file imports the required modules directly. This tells Webpack exactly which dependencies it needs to process and bundle.
For example, instead of manual linking, you import the module within your main application file:
// resources/assets/js/app.js
// Import Moment.js directly as a module
import moment from 'moment';
// Now you can use the imported object
const today = moment().format('YYYY-MM-DD');
console.log(today);
// ... rest of your application logicStep 2: Configure Laravel Mix to Handle Assets
Your webpack.mix.js file acts as the bridge between your source code and the final public assets. It tells the bundler how to compile everything into the desired output directory. Since you are using a modern asset pipeline, the bundling process automatically scans for dependencies defined in your JS files.
// mix.js
let mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css'); When you run npm run dev, Mix executes Webpack, which reads your app.js. Because app.js contains the import moment from 'moment'; statement, Webpack knows it must resolve and bundle the necessary code from the node_modules directory into the final /public/js folder.
Conclusion: Build Over Manual Inclusion
The takeaway here is that for any complex dependency management in a Laravel project, especially when dealing with front-end libraries, never attempt to manually link files from node_modules. This approach is fragile and defeats the purpose of using modern build tools.
By adopting a robust asset bundling strategyâusing Webpack/Mix to manage importsâyou ensure that your application remains portable, optimized, and adheres to best practices for large-scale development, perfectly aligning with the principles promoted by laravelcompany.com. Focus on managing what you need (imports) rather than manually managing where the files are located.