rollup-plugin-svelte
rollup-plugin-svelte copied to clipboard
Build fails with "flatted" dependency: './package.json' is not defined by "exports"
Hello!
Recently added a new ultity flatted to my project to handle some circular JSON. Unfortunately, this seems to have broken the build. I get the following error, and the build fails:
[!] (plugin svelte) Error: Package subpath './package.json' is not defined by "exports" in /Users/zac/Code/mfe-dashboard/node_modules/flatted/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/zac/Code/mfe-dashboard/node_modules/flatted/package.json
at throwExportsNotFound (internal/modules/esm/resolve.js:299:9)
at packageExportsResolve (internal/modules/esm/resolve.js:522:3)
at resolveExports (internal/modules/cjs/loader.js:449:36)
at Function.Module._findPath (internal/modules/cjs/loader.js:489:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:875:27)
at Function.requireRelative.resolve (/Users/zac/Code/mfe-dashboard/node_modules/require-relative/index.js:30:17)
at tryResolve (/Users/zac/Code/mfe-dashboard/node_modules/rollup-plugin-svelte/index.js:50:19)
at Object.resolveId (/Users/zac/Code/mfe-dashboard/node_modules/rollup-plugin-svelte/index.js:177:21)
at /Users/zac/Code/mfe-dashboard/node_modules/rollup/dist/shared/node-entry.js:13117:25
Found a discussion of a similar issue with uuid. Seems like flatted could fix this by adding "./package.json": "./package.json"
to their package.json... but is there no way Svelte rollup can handle this more gracefully? Maybe throw a warning but find the package.json procedurally as a fallback? Flatted is a fairly well used utility (23 million downloads a week), and it seems strange Svelte simply would not support it over what appears to be a fairly small issue.
If a library has exports
in its package.json
, that indicates to Node that those are the only things that can be imported or required from the package, and if it doesn't include package.json
, we can't access whatever/package.json
.
I'm not sure there's any reasonable for the Rollup plugin to do here to access this, other than to completely sidestep Node's module resolution and manually load this file and parse it, which doesn't sound ideal.
If a package decides to use pkg.exports
, it really is kind of on them to expose package.json
because there are a lot of valid reasons that other packages might want to be able to access that. I'd suggest an issue or PR with flatted to add that file to their exports map.
PR here: https://github.com/WebReflection/flatted/pull/54
Hey, I appreciate the fast response and PR!
This all sounds reasonable. I agree that it seems like more of a flatted problem than a rollup problem, but I do wonder how many other major libraries out there fall into this trap? It may not be ideal to rely on each individual maintainer to merge updates before a library can be used with Svelte. Not saying the trade offs of some edge case workaround are necessarily worth it either, just that it may be worth thinking through.
We're going to change the behavior related to this. In the meantime, closing as a duplicate of https://github.com/sveltejs/rollup-plugin-svelte/issues/181