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

export map breaks rollup plugin, others trying to import/require ('svelte/package.json') #5659

Closed
FredKSchott opened this issue Nov 9, 2020 · 12 comments · Fixed by #5660
Closed

Comments

@FredKSchott
Copy link

Congrats on releasing the new export map! Quick issue spotted: Node.js doesn't default to include the package.json file as part of your package, meaning tooling/consumers can't `require('svelte/package.json'). The Rollup plugin currently does this, as do many others.

Snowpack's Svelte template is broken as a result.

The fix is to add this to your export map:

"exports": {
+    "./package.json": "./package.json"
}
@FredKSchott
Copy link
Author

Since this went out as a patch release, I'd recommend a quick follow up patch to add.

@bradak1
Copy link

bradak1 commented Nov 9, 2020

** RUNNING ROLLUP: prod: true **
Bundling: dynamic
[!] Error: Package subpath './package.json' is not defined by "exports" in <path>/site/node_modules/svelte/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in <path>/site/node_modules/svelte/package.json

@FredKSchott
Copy link
Author

@MylesBorins is there a thread where this "package.json not automatically included in export map" behavior is already being discussed? I've seen this issue bite so many packages trying to upgrade to an export map, and it's only going to keep happening.

If it hasn't been proposed yet, I'd like to start a thread.

@Conduitry
Copy link
Member

Thanks! Fixed in 3.29.6 - https://unpkg.com/svelte@3.29.6/package.json

@bradak1
Copy link

bradak1 commented Nov 9, 2020

not fixed, now I get error for compiler.js:

** RUNNING ROLLUP: prod: true **
Bundling: dynamic
[!] Error: Package subpath './compiler.js' is not defined by "exports" in <path>/site/node_modules/svelte/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './compiler.js' is not defined by "exports" in <path>/site/node_modules/svelte/package.json

@Conduitry
Copy link
Member

The export map contains

    "./compiler": {
      "import": "./compiler.mjs",
      "require": "./compiler.js"
    },

Which I assume means that things need to use svelte/compiler and that something is using svelte/compiler.js. Do we need to also support svelte/compiler.js? In your case, what's trying to import/require that? Is it the Rollup plugin?

@bradak1
Copy link

bradak1 commented Nov 9, 2020

yes, rollup

@Conduitry
Copy link
Member

Yeah, I see it at https://github.com/sveltejs/rollup-plugin-svelte/blob/65fbfc7824b7e86e546ed064bf6d94b87ebb7b37/index.js#L15 - This will be fixed in the next major version of the Rollup plugin, but we should either also support importing from svelte/compiler.js in the export map, or we should backport a fix to 6.x of the Rollup plugin to use svelte/compiler. I don't know which is more proper.

@Conduitry
Copy link
Member

This should be fixed with rollup-plugin-svelte@6.1.1.

@bradak1
Copy link

bradak1 commented Nov 9, 2020

yup, it's fixed. Thx!

@MylesBorins
Copy link

MylesBorins commented Nov 9, 2020

@MylesBorins is there a thread where this "package.json not automatically included in export map" behavior is already being discussed? I've seen this issue bite so many packages trying to upgrade to an export map, and it's only going to keep happening.

If it hasn't been proposed yet, I'd like to start a thread.

Oh hi!

There is a historical thread nodejs/modules#445

TL;DR while it is "unexpected" today, the general intent of exports is to make the contract for a module explicit, and the general thought was that exposing the package.json as part of the module API should be opt-in by the module author

Making a carve out in exports for package.json imho complicates the API, and while there is a mismatch with current expectation, it would be more confusing in the long term.

One solution that has been discussion is exposing an API in Node.js core that would make it easy for tools / module consumers to easily get the path to a package.json. This is being discussed in nodejs/node#49445

With this type of util api you would be able to do something like

import { readFile } from 'fs/promises';
import { resolvePackageRoot } from 'module';
const svelteRoot = await resolvePackageRoot('svelte');
const svelteJSON = JSON.parse(await readFile(svelteRoot + '/package.json'));

While this is definitely more verbose it would be consistent and reliable for all packages while still allowing a module author to not make the package.json part of their packages API by default.

@FredKSchott
Copy link
Author

Thanks @MylesBorins, responded in historical thread.

fregante added a commit to refined-github/github-url-detection that referenced this issue Nov 10, 2020
Build silently failing due to sveltejs/svelte#5659
niedzielski added a commit to niedzielski/browser-fs-access that referenced this issue Jun 6, 2021
Update package.json exports to include package.json. Why package.json
needs to export itself, I cannot say but it fixes the following Svelte
rollup.js plugin warning:

  [rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.

  - browser-fs-access

I've verified locally using `npm link` that the warning no longer occurs
with this change in my Svelte project. I _believe_ the otherwise
preexisting exports behavior is preserved.

References:
- [Official documentation](https://nodejs.org/api/packages.html#packages_exports) and [much more useful documentation with examples from the proposal](https://github.com/jkrems/proposal-pkg-exports)
- [Similar issue for a different package](sveltejs/svelte#5659) and [its resolution](https://unpkg.com/svelte@3.29.6/package.json)
Morita0711 added a commit to Morita0711/browser-file-operation that referenced this issue Jul 3, 2022
Update package.json exports to include package.json. Why package.json
needs to export itself, I cannot say but it fixes the following Svelte
rollup.js plugin warning:

  [rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.

  - browser-fs-access

I've verified locally using `npm link` that the warning no longer occurs
with this change in my Svelte project. I _believe_ the otherwise
preexisting exports behavior is preserved.

References:
- [Official documentation](https://nodejs.org/api/packages.html#packages_exports) and [much more useful documentation with examples from the proposal](https://github.com/jkrems/proposal-pkg-exports)
- [Similar issue for a different package](sveltejs/svelte#5659) and [its resolution](https://unpkg.com/svelte@3.29.6/package.json)
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

Successfully merging a pull request may close this issue.

4 participants