How do I require npm packages after installation in Laravel?

How do I require npm packages after installation in Laravel?

For example, I need a package sweetalert2

that first installs it:

npm install --save sweetalert2

      

Now, do I need to require it in a file \resources\assets\js\bootstrap.js

in laravel?

This default content looks like this:

window._ = require('lodash');

try {
    window.$ = window.jQuery = require('jquery');

    require('bootstrap');
} catch (e) {}


window.axios = require('axios');

window.axios.defaults.headers.common['X-CSRF-TOKEN'] = window.Laravel.csrfToken;
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

      

It seems that in laravel required lodash

, jquery

, bootstrap

, axios

, but offers 4 different from each other:

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.axios = require('axios');

      

Questions:

1, why 4 sentences have differences?

2, I want to claim a package sweetalert2

, how to write it?

+3


source to share


1 answer


You should just be able to require

in the file bootstrap.js

:

require('sweetalert2');

      

Then (assuming you are using scss

) in your post. scss file:



@import "node_modules/sweetalert2/src/sweetalert2";

      

Also, the reason you don't need to manually attach bootstrap

to window

is because it will do it automatically on boot. This is because it is assumed that the instance window

will always exist as it is a style structure. One of the reasons other libraries might not do this is because they might be used in environments where it is window

not defined.

Hope this helps!

+3


source







All Articles