2

I am new to Laravel and following Laracast tutorials for Laravel 5.4
I read about Laravel mix and how we can compile our assets using Webpack Laravel Mix tool. So I tried adding a JavaScript Package

1- Installed AlertifyJS using npm.
2- Added require('alertifyjs') in resources\assets\js\bootstrap.js
3- Executed npm run dev. Assets were compiled to public\js\app.js , where I can see alertifyjs code by finding alertify keyword.

I used alertify code like below in resources\assets\js\app.js:

`$(document).ready(function(){
    $('.run').on('click' , function(event){
        alertify.alert("This is an alert dialog.", function(){
        alertify.message('OK');
    });
  });
});`


View File:

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading text-center"><h2>THIS WILL BE THE HOME PAGE<h2></div>
                <div class="panel-body text-center">
                    Home Page Content Goes Here!
                </div>
                            <button class="run">Show Alert</button>
            </div>
        </div>
    </div>
</div>
@endsection


Problem: When I click on button Show Alert ,
alertify is not defined errors is logged to console. Am I missing something while compiling assets?

miken32
  • 42,008
  • 16
  • 111
  • 154
awebartisan
  • 1,424
  • 16
  • 27
  • Is your app.js file being included in html in either of your blade components? – slapbot Mar 03 '17 at 20:42
  • yes. `app.blade.php` , its layout file – awebartisan Mar 03 '17 at 20:57
  • somehow, `alertify's` all code was compiled and put just under my in `public\js\app.js`, that's why `undefined` error is being thrown – awebartisan Mar 03 '17 at 20:58
  • Also, although you can use `alertify` on its own with laravel, its a bit tedious. You can use a `laravel-alertify` package I just published: https://github.com/odannyc/laravel-alertify – odannyc Jul 19 '17 at 15:05
  • How did you import the `alertify.min.css` and `themes/bootstrap.min.css` files? – Pathros Aug 07 '20 at 01:43

1 Answers1

5

You need to require alertify in your app.js, where you're actually using it. Webpack keeps each file as a module, just puts them together in one file (the bundle). But you're expecting it to be available globally, which is not a good idea and modules that do depend on globals (like jQuery plugins) are referred to as legacy modules in the official webpack docs. Have a look at Shimming for more details on such legacy modules.

Anyway, that is not the case for you, but just keep in mind that modules you install with npm should always be required in the file you're actually using them. Modules have their own scope, it's not just putting the files together when you require them. If this concept of modules is new to you or you'd like to understand it better you should read Node.js Modules, because this is heavily used in JavaScript.

Your resources\assets\js\app.js needs to be changed to:

const alertify = require('alertifyjs');

$(document).ready(function(){
  $('.run').on('click' , function(event){
    alertify.alert("This is an alert dialog.", function(){
      alertify.message('OK');
    });
  });
});
Michael Jungo
  • 31,583
  • 3
  • 91
  • 84
  • 1
    Thanks for this answer. It helped me as well, but I am hoping you could explain a bit more. I'm trying to include Backbone into my existing Laravel Spark application which is becoming an SPA very quickly. Spark includes multiple libraries from NPM using code like: `window._ = require('underscore');` When I try this with: `window.Backbone = require('backbone');` It doesn't work. Backbone is undefined. Why would it exhibit that behavior? Just curious. – TomWilsonFL Mar 08 '17 at 18:04
  • Further, if you do not register some modules with the root, how do you override functions such as Backbone.sync which are used throughout? – TomWilsonFL Mar 08 '17 at 18:17
  • @TomWilsonFL 1) `window.Backbone = require('backbone')` should make it available globally in the browser, just needs to be included before the files that actually use it. 2) It is generally not a good idea to overwrite functions of a library, this is known as [Monkey patching](https://en.wikipedia.org/wiki/Monkey_patch). – Michael Jungo Mar 08 '17 at 21:25
  • It actually worked for me as `window.alertify = require('alertifyjs');`. But now, how do you import the `alertify.min.css` and `themes/bootstrap.min.css` files? Inside the `app.scss` file I have tried `@import '~alertifyjs/build/css/alertify.min.css';` but the CSS is not working. The alert shows up as pure text, without the CSS :´( – Pathros Aug 07 '20 at 01:44