greensock / GSAP

GSAP (GreenSock Animation Platform), a JavaScript animation library for the modern web
https://gsap.com
19.94k stars 1.72k forks source link

Jest unit test import fails with SyntaxError: Unexpected token export at export var ScrollToPlugin #490

Closed olignyf closed 2 years ago

olignyf commented 2 years ago

I've been having this error and wonder if someone found a fix for it? gsap 3.9.1 This happens in Jest unit tests


    node_modules\gsap\ScrollToPlugin.js:127
    export var ScrollToPlugin = {
    ^^^^^^

    SyntaxError: Unexpected token export

      1 | import { gsap } from 'gsap';
    > 2 | import ScrollToPlugin from 'gsap/ScrollToPlugin';
        | ^```
jackdoyle commented 2 years ago

That error sounds like an issue in your setup not recognizing ES Modules (unrelated to GSAP). I'm not familiar with Jest but it sure sounds like you just need to tweak something in your environment so that you're either using the standard ES5 files (in the /dist/ directory) or make sure your tooling is configured to allow ES Modules ("import" and "export" are hallmarks of ES Modules).

olignyf commented 2 years ago

Thanks for your reply to narrow down the issue to ES modules. But I have 36 main packages dependencies and gsap is the only one producing this error. I am importing with "import" and "export" in hundreds of files with other packages and it works well.

jackdoyle commented 2 years ago

Hi @olignyf It's virtually impossible for us to troubleshoot blind. I'm relatively confident this has nothing to do with GSAP because thousands of people are using it every day (11+ million sites) and nobody else is reporting something similar.

Like I said, that error sure sounds like an indication that your particular environment isn't set up to handle ES Modules. Perhaps the other libraries you're using aren't using ES Modules by default(?) Have you tried importing the ES5 files instead for GSAP (in the /dist/ directory) like:

import gsap from "gsap/dist/gsap"; import ScrollToPlugin from "gsap/dist/ScrollToPlugin"; ...

?

OSUblake commented 2 years ago

Just because you are using import does not mean you are importing an ES Module. Tools like webpack and babel have really blurred the lines of what is actually an ES Module or just CommonJS module.

According to this, it says that ES Module support is experimental.

https://jestjs.io/docs/ecmascript-modules

So if you're getting that error, you should import the UMD/CommonJS files from the dist folder.