Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular 5.2.3 - ERROR TypeError: sweetalert_1.default is not a function #799

Open
softsaravanacse opened this issue Feb 6, 2018 · 13 comments

Comments

@softsaravanacse
Copy link

softsaravanacse commented Feb 6, 2018

I am getting this error while using es5. But it works fine on es6.

sweetalert version : 2.1.0

tsconfig.json

{
    "compileOnSave": false,
    "compilerOptions": {
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "target": "es5",
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2015",
            "dom"
        ]
    }
}
import swal from 'sweetalert';
swal({
             text: 'Your request has been processed successfully.',
             icon: 'success'
           }).then((value) => {
             if (value) {
                 console.log(value);
             }
           });
@dalvarezr
Copy link

I started having this error when I uncommented the following lines so that my project could be compatible with IE9, 10 and 11. Located in polyfills.ts
import 'core-js/es6/symbol';
import 'core-js/es6/object';
import 'core-js/es6/function';
import 'core-js/es6/parse-int';
import 'core-js/es6/parse-float';
import 'core-js/es6/number';
import 'core-js/es6/math';
import 'core-js/es6/string';
import 'core-js/es6/date';
import 'core-js/es6/array';
import 'core-js/es6/regexp';
import 'core-js/es6/map';
import 'core-js/es6/weak-map';
import 'core-js/es6/set';
import 'core-js/es7/array';

Changing the "target" to es6 as you suggest breaks my app on IE 11 since apparently es6 arrow functions aren't yet supported on IE 11 and compiling my project will throw an error.

@pedro412
Copy link

Having same error

image

@acib708
Copy link

acib708 commented Mar 1, 2018

+1

@mpintener
Copy link

I'm having the same error!

@greatkeke
Copy link

me,too.

@dalvarezr
Copy link

I solved the issue by swaping to sweetalert 2. Im not sure if its developed by the same team (so im sorry for the propaganda if not).
Hope it can be useful for those having the same issue and I'd be glad to swich back to swal 1 if this gets fixed.
Happy coding!

@lionralfs
Copy link
Collaborator

Hey guys.
This seems to be related to this bug in Angular CLI where default exports are handled differently in dev. They seem to have fixed it in v6.0.0-beta.4

Do you still get any errors when running ng serve --prod?

Alternatively, you could use this somewhat dirty workaround:

import * as _swal from 'sweetalert';
import { SweetAlert } from 'sweetalert/typings/core';
const swal: SweetAlert = _swal as any;

swal('test');

@greatkeke
Copy link

@lionralfs It works fine.

@mombe090
Copy link

mombe090 commented Apr 12, 2018

@lionralfs you are a true lion work fine.

@pluma
Copy link

pluma commented Sep 13, 2018

The npm release doesn't export a default property, so the typings are wrong. The typings indicate it should be treated as an ES module, but the npm release is a CommonJS module.

The documentation suggests to use export = instead of export default in this case: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require

The matching import looks like this:

import swal = require("sweetalert");

Yes, it's nonstandard syntax, which will cause problems for users targeting ES modules. By enabling synthetic imports or using the import * as swal syntax it's possible to work around this, but the proper solution would be either to correct the typings or simply add module.exports.default = module.exports to the end of the main JS file so people can import it as an ES module.

@ya1k96
Copy link

ya1k96 commented Jan 21, 2019

Great! im done

@Br0wnZ
Copy link

Br0wnZ commented Nov 21, 2019

Edit node_modules/sweetalert/typings/sweetalert.d.ts from...
`import swal, { SweetAlert } from "./core";

declare global {
const swal: SweetAlert;
const sweetAlert: SweetAlert;
}

export default swal;
export as namespace swal;`

to...

`import swal, { SweetAlert } from "./core";

export default swal;
export as namespace swal;`

@Janaka-Steph
Copy link

I had the same issue with ReactJS, but using https://github.com/sweetalert/sweetalert-with-react fixed it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests