9

How do you include /node_modules in a laravel 5.5 application, i referenced this https://laravel.com/docs/5.5/mix

i can do this

npm install angular-moment moment --save

and it will make a /node_modules directory, however laravel ignores it

lets say i want to do this

<script src="node_modules/moment/moment.js"></script>

it will not work, it will show an error

i currently have this in my webpack, what will be the best way to integrate it ?

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');
BARNOWL
  • 3,326
  • 10
  • 44
  • 80

1 Answers1

15

Just include it in your app.js so it is complied in...

var moment = require('moment');

Then access it as moment... like

moment().format();
Serge
  • 2,107
  • 1
  • 18
  • 24
  • 1
    Yeah, and just make sure in your main php file where you are putting your compiled scripts you use something like `src="{{mix('build/app.js')}}"` in the script to access it properly. – Matt McAlister Nov 25 '17 at 04:28
  • hmmm let me try it out – BARNOWL Nov 25 '17 at 04:35
  • @serge then do i still do this `` – BARNOWL Nov 25 '17 at 05:18
  • Seems like assigning to the variable is a MUST, if I just required my node module `require('MyModule')`, it didn't work - when I assigned it to the variable which I was expecting - `MyModule = require('MyModule');` - this worked – jave.web May 22 '19 at 09:19