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

Cannot load metadata from ESM package #753

Open
timdp opened this issue Sep 27, 2022 · 2 comments
Open

Cannot load metadata from ESM package #753

timdp opened this issue Sep 27, 2022 · 2 comments

Comments

@timdp
Copy link

timdp commented Sep 27, 2022

Bug Description

utils.loadModuleData() is defeated b yESM packages.

Code snippets

{
  "name": "depcheck-test",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "test": "depcheck .",
    "foo": "miniflare baz"
  },
  "dependencies": {
    "depcheck": "^1.4.3",
    "miniflare": "^2.9.0"
  }
}

This crashes the require.resolve('.../package.json') call in loadModuleData with:

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/timdp/Code/depcheck-test/node_modules/miniflare/package.json
    at new NodeError (node:internal/errors:393:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:340:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:619:3)
    at resolveExports (node:internal/modules/cjs/loader:492:36)
    at Module._findPath (node:internal/modules/cjs/loader:532:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:941:27)
    at Function.resolve (node:internal/modules/cjs/helpers:109:19)
    at loadModuleData (/Users/timdp/Code/depcheck-test/node_modules/depcheck/dist/utils/index.js:48:26)
    at getCacheOrLoad (/Users/timdp/Code/depcheck-test/node_modules/depcheck/dist/special/bin.js:22:48)
    at getBinaries (/Users/timdp/Code/depcheck-test/node_modules/depcheck/dist/special/bin.js:31:23) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

because Miniflare uses ESM exports in package.json, which require.resolve() respects.

Theoretically, Miniflare could export package.json explicitly, but there's no need for that. I would recommend using read-pkg instead of require.resolve() + require().

Versions

  • node -v: v18.8.0
  • npm -v: 8.18.0
  • depcheck --version: 1.4.3
@timdp timdp added the bug label Sep 27, 2022
@dword-design
Copy link
Contributor

I created a PR that fixes it #760.

@stale
Copy link

stale bot commented Jun 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants