sweetalert2: "swal is not defined" after migrating from Sweet Alert 1

I was previously using the original Sweet Alert, and things were working fine. Upgraded to SA2 and followed the migration guide. My code:

import swal from "sweetalert2"

swal({
    title: "Are you sure?",
    text: "This cannot be undone.",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes.",
    closeOnConfirm: false,
    showLoaderOnConfirm: true
}).then(function() {
    /* do stuff */
})

This results in swal is not defined.

Webpack doesn’t report an error, so it’s not a compilation issue. Do I have to do something else to make this work? Other than switching from callbacks to promises as per the migration guide, nothing else has changed in my code. Using Chrome.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

import swal from 'sweetalert2';

window.swal = swal

i had resolved this issue by replacing sweetalert2 cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.js"></script>

For anyone struggling with this, I finally resolved it by re-importing swal in each component in which it is invoked. Importing it just in main.js didn’t work.

I use Vue.js. Did not have this problem with the original SweetAlert.

Just in case you’re wondering, this is normal and indicates that your module bundler does its job correctly, and that swal is no longer a global object manually attached to the window, but behaves like a real ES 2015 module.

@GarbledTTT the cloudflare CDN is not the official one. Please use the one pointed by limonte above:

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>

i had resolved this issue by replacing sweetalert2 cdn

<script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.29.2/sweetalert2.all.js"></script>

Thank you 😃 its also worked for me

import swal from ‘sweetalert2’;

window.swal = swal

This did it for me

@egeersoz I just updated https://github.com/limonte/sweetalert2-webpack-demo to latest versions, works fine, take a look.