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

fix: specify './package.json' in 'exports' #441

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tchupp
Copy link

@tchupp tchupp commented Aug 31, 2021

Some javascript bundlers use require.resolve to identify the package.json. If the package.json can't be resolved, the dependency isn't included.

Specifically in my case, generate-package-json-webpack-plugin was having trouble locating the package.json due to the export fields.

Test case, before:

$ npm i graphql

$ npm i graphql-middleware

$ node

> require.resolve("graphql/package.json", { paths: ["/Users/theochupp/Documents/graphql-middleware-test/apps/graphql/src"], })
'/Users/theochupp/Documents/graphql-middleware-test/node_modules/graphql/package.json'

> require.resolve("graphql-middleware/package.json", { paths: ["/Users/theochupp/Documents/graphql-middleware-test/apps/graphql/src"], })
Uncaught:
Error: Cannot find module '/Users/theochupp/Documents/graphql-middleware-test/node_modules/graphql-middleware/dist/package.json.js'
    at createEsmNotFoundErr (internal/modules/cjs/loader.js:842:15)
    at finalizeEsmResolution (internal/modules/cjs/loader.js:835:15)
    at resolveExports (internal/modules/cjs/loader.js:424:14)
    at Function.Module._findPath (internal/modules/cjs/loader.js:464:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:802:27)
    at Function.resolve (internal/modules/cjs/helpers.js:80:19) {
  code: 'MODULE_NOT_FOUND',
  path: '/Users/theochupp/Documents/graphql-middleware-test/node_modules/graphql-middleware/package.json'
}

Test case, after:

$ npm i graphql

$ npm i graphql-middleware

$ node

> require.resolve("graphql/package.json", { paths: ["/Users/theochupp/Documents/graphql-middleware-test/apps/graphql/src"], })
'/Users/theochupp/Documents/graphql-middleware-test/node_modules/graphql/package.json'

> require.resolve("graphql-middleware/package.json", { paths: ["/Users/theochupp/Documents/graphql-middleware-test/apps/graphql/src"], })
'/Users/theochupp/Documents/graphql-middleware-test/node_modules/graphql-middleware/package.json'

I'm not sure this is even worth adding a unit test for 😂

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 this pull request may close these issues.

1 participant