-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
package.json
missing "type" / "exports"
#548
Comments
Hi @fractalhq Can you please explain why you think these are necessary/appropriate? Maybe provide links to supporting docs/articles? |
Hey! Sorry, it may not be that simple as I've yet to find time to fork and test it, but you can see more detailed errors regarding the module's configuration by using this great tool from one of the core Typescript engineers in charge of module resolution: |
Here is the documentation: And the section specific to this particular kerrfuffle: |
Yeah, sorry, it's not at all clear to me what exactly the problem is or the specific solution required. Maybe I'm missing something obvious (to be honest, I don't have a lot of time to do extensive reading on it at the moment). Have you tested your proposed solution? Do you have a repo that illustrates (in simple form) it broken and then it being resolved by your suggestion? I've been burned before by adding/changing stuff in the package.json that might have seemed fine in one context but it broke things for many other people, so I'm very cautious about making tweaks like this. |
cjsThese pkg options (main,module,types) are intended for older versions of Node and TS (legacy versions) that used // package.json
{
"main": "./dist/index.js"
"module": "./dist/index.mjs"
"types": "./dist/index.d.ts"
} // require
const { gsap } = require('gsap') esmThe At the moment, // package.json
{
"type": "module",
"exports": {
".": {
"types": "./dist/types/index.d.ts"
"import": "./dist/esm/gsap-core.mjs",
"require": "./dist/cjs/gsap-core.cjs",
},
"./ScrollTrigger": {
"types": "./dist/types/scroll-trigger.d.ts"
"import": "./dist/esm/scroll-trigger.mjs",
"require": "./dist/cjs/scroll-trigger.cjs",
},
// ...
}
} // import
import { gsap } from 'gsap'
import { ScrollTrigger } from 'gsap/ScrollTrigger' tsAlso, in TypeScript projects, the // tsconfig.json
{
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "NodeNext",
// ...
},
// ...
} // "moduleResolution": "Node" (legacy)
import { module } from './dir/subdir' // "moduleResolution": "NodeNext"
import { module } from './dir/subdir/index.js' breaking changesKeep in mind that these changes are And to be clear, there are no changes to the production code (core or plugins). Changes are needed in the project/pkg setup. At some point it will be necessary to migrate all the For example, |
Thanks for the info, @ivodolenc I think it would be very unwise for us to implement breaking changes at this point. With 12 million sites using GSAP, there are a lot of people that would probably be very upset if we imposed breaking changes like this. One CDN alone sometimes gets 11+ Billion requests for GSAP per month. And those are exclusively for the CJS files, not ES Modules. So if the change you're suggesting requires us to only provide ES Modules in the package, that's definitely problematic. Is there something we can do that would help your scenario without implementing breaking changes? It would also be helpful if you could summarize the problem that the current package.json is causing for you (and others), and an easy way to reproduce it in the most minimal way possible. |
Yep, we build in ESM and use Rollup to generate cjs. So our package already delivers ESM...I guess I'm just a bit confused about what exactly the issue is here that causes any problems with the current setup. Are you just suggesting that we add the "exports" for every single JS file (which requires 3 entries for each)? And even doing that may cause breaking changes? |
If you are not familiar with the It's was similar with TypeScript, breaking changes were made by the official team when they introduced module resolution It can all be complicated and confusing at first, but you should definitely take the time to research it in more detail. The current gsap setup works fine, but it is adapted for older legacy versions that are no longer officially supported by node and typescript. Latest active stable version of node is In version So this can give you some solid guidance, for which versions you actually want to develop gsap further. At some point, if you're going to migrate to newer versions of node and typescript, you'll need to implement an |
For those running into the following build error with Astro (and possibly Vite) projects that import GSAP modules, there is a workaround using patch-package to configure the
Once you have installed patch-package, open the project's {
+ "type": "module",
+ "exports": {
+ ".": {
+ "import": {
+ "types": "./types/index.d.ts",
+ "default": "./index.js"
+ }
+ },
+ "./ScrollTrigger": {
+ "import": {
+ "types": "./types/scroll-trigger.d.ts",
+ "default": "./ScrollTrigger.js"
+ }
+ }
+ // and other plugins...
+ },
"name": "gsap",
"version": "3.12.5",
"description": "GSAP is a framework-agnostic JavaScript animation library that turns developers into animation superheroes. Build high-performance animations that work in **every** major browser. Animate CSS, SVG, canvas, React, Vue, WebGL, colors, strings, motion paths, generic objects...anything JavaScript can touch! The ScrollTrigger plugin lets you create jaw-dropping scroll-based animations with minimal code. No other library delivers such advanced sequencing, reliability, and tight control while solving real-world problems on millions of sites. GSAP works around countless browser inconsistencies; your animations **just work**. At its core, GSAP is a high-speed property manipulator, updating values over time with extreme accuracy.", Then run If you are also using {
+ "type": "module",
+ "exports": {
+ ".": {
+ "import": {
+ "types": "./types/index.d.ts",
+ "default": "./src/index.js"
+ }
+ }
+ },
"name": "@gsap/react",
"version": "2.1.0",
"description": "Tools for using GSAP in React, like useGSAP() which is a drop-in replacement for useLayoutEffect()/useEffect()", Then run Now, the build should work. Note that you must recreate the patches every time you update the package versions. |
Hi @chalkygames123 Thanks so much for the detailed solution. I'm just curious if you tried importing from the /dist/ directory. For environments that don't understand ES Modules, you should be able to just switch to the UMD versions like: // ES Module
import ScrollTrigger from "gsap/ScrollTrigger";
// UMD
import ScrollTrigger from "gsap/dist/ScrollTrigger"; // <-- in the /dist/ folder |
@jackdoyle Ah, yes, I just tried it and it does work for sure, and I just found the documentation about that (https://gsap.com/docs/v3/Installation#faqs). That said, it is still just another workaround, I guess. FYI, you may find publint useful for configuring npm packages correctly. For example: |
Thanks for the clarification. I'm curious - if this is added to the package.json (with no other changes), does it resolve your issue (without needing to import from /dist/)? "type": "module" I'm just a bit concerned that if the package technically has BOTH a module (default) version AND a commonjs (/dist/) version, I wouldn't want the /dist/ version to suddenly not work because it's forced everything to be interpreted as modules due to that type declaration. See what I mean? |
In that case, I will end up with getting the following error with
and with
For your concern, I would recommend reading the official Node.js docs, especially the "Dual CommonJS/ES module packages" and the "Conditional exports" sections. |
I believe these fields need to be added to your
package.json
:The text was updated successfully, but these errors were encountered: