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

package.json is not defined by "exports" #444

Closed
evdama opened this issue May 11, 2020 · 27 comments · Fixed by #449
Closed

package.json is not defined by "exports" #444

evdama opened this issue May 11, 2020 · 27 comments · Fixed by #449

Comments

@evdama
Copy link

evdama commented May 11, 2020

Describe the bug

'./package.json' is not defined by "exports" when upgrading from v7.0.3 to v8.0.0

To Reproduce

Steps to reproduce the behavior:

  1. upgrading from v7.0.3 to v8.0.0 and running npm build in a Svelte project
  2. See error

Expected behavior

A rollup bundle without an error as shown below.

Runtime

  • OS: macOS
  • Runtime: Node.js
  • Runtime Version: 14.1.0

Additional context

This is the command line output after upgrading from v7.0.3 to v8.0.0 in a Svelte project

> Package subpath './package.json' is not defined by "exports" in /Users/sa/0/edm/node_modules/uuid/package.json
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /Users/sa/0/edm/node_modules/uuid/package.json
    at applyExports (internal/modules/cjs/loader.js:491:9)
    at resolveExports (internal/modules/cjs/loader.js:507:23)
    at Function.Module._findPath (internal/modules/cjs/loader.js:635:31)
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1007:27)
    at Function.requireRelative.resolve (/Users/sa/0/edm/node_modules/require-relative/index.js:30:17)
    at tryResolve (/Users/sa/0/edm/node_modules/rollup-plugin-svelte/index.js:50:19)
    at Object.resolveId (/Users/sa/0/edm/node_modules/rollup-plugin-svelte/index.js:177:21)
    at /Users/sa/0/edm/node_modules/rollup/dist/shared/rollup.js:18217:25

I found some additional information in a SO post that might very well be related to the issue I'm seeing https://stackoverflow.com/questions/61621828/getting-strange-error-using-uuid-npm-module-what-am-i-doing-wrong/61730597#61730597

@ctavan
Copy link
Member

ctavan commented May 11, 2020

@evdama could you provide actual steps that let me reproduce your error (meaning, code that I can execute)? "grading from v7.0.3 to v8.0.0 and running npm build in a Svelte project" does not really help since I have no clue about the details of your project…

@evdama
Copy link
Author

evdama commented May 11, 2020

I'm using uuid inside my server.js like so

import { v4 as uuidv4 } from 'uuid'

... more code...


const setNonceMiddleware = ( request, response, next ) => {
  try {
    response.locals.nonce = uuidv4()
    next()
  } catch ( error ) {
    console.error ( 'error from setNonceMiddleware: ', error )
  }
}

I don't use uuid anywhere else in my app, just the snippet above. I'll also provide a gist of my rollup config that might help, it's a Sapper (Svelte) project with a client and a server as well as a serviceworker section: https://gist.github.com/7b56754df52b5b26b1db3f2873cee726

@ctavan
Copy link
Member

ctavan commented May 11, 2020

OK, this is strange.

Judging from the stack trace of your error this looks much more like an issue with https://github.com/sveltejs/rollup-plugin-svelte though…

Apparently the svelte rollup plugin uses require-relative under the hood:

    at Function.requireRelative.resolve (/Users/sa/0/edm/node_modules/require-relative/index.js:30:17)

which likely has problems with modern Node.js module loading since its latest version has been published 6 years ago:

image

I don't think we could or should solve this in this library here. I think the right place to report this issue would be https://github.com/sveltejs/rollup-plugin-svelte/issues

@evdama
Copy link
Author

evdama commented May 11, 2020

@ctavan thanks much, I'm trying to catch one of the svelte devs (@Conduitry) on discord, ask him what he thinks is best i.e. me filling an issue about an outdated require-relative that's used in rollup-plugin-svelte

@ctavan
Copy link
Member

ctavan commented May 11, 2020

@evdama I dug a little bit into it, I think it's a problem directly in the svelte rollup plugin, see sveltejs/rollup-plugin-svelte#104

@Danny-A
Copy link

Danny-A commented May 12, 2020

It is not only a Svelte Rollup problem.

I upgraded to v8.0.0 in my React Native project and encountered the exact same problem.

@ctavan
Copy link
Member

ctavan commented May 12, 2020

@Danny-A can you specify what you mean by "exact same problem"? Do you get exactly the same stack trace? Do you use svelte and rollup-plugin-svelte within react-native (is that even possible)?

@ctavan
Copy link
Member

ctavan commented May 12, 2020

@TrySound I think you are more familiar with use cases of requiring package.json files from npm modules in bundler pipelines. Is this something that npm modules should generally support? Is this a documented requirement for e.g. rollup?

@Danny-A
Copy link

Danny-A commented May 12, 2020

@ctavan Sorry for the lack of context. I get a Warning in RN. I don't get a stack trace and I am not using rollup.

warn Package uuid has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in /Users/danny/Projecten/***/mobile/node_modules/uuid/package.json

@TrySound
Copy link
Member

TrySound commented May 12, 2020

There is no any requirement for bundlers because they mostly do not support native node modules. Perhaps package.json should always be in exports list. See here
ai/nanoevents#44 (comment)
https://unpkg.com/nanoevents@5.1.6/package.json

ctavan added a commit that referenced this issue May 12, 2020
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well.

See also:
ai/nanoevents#44 (comment)

Fixes #444
ctavan added a commit that referenced this issue May 12, 2020
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well.

See also:
ai/nanoevents#44 (comment)

Fixes #444
ctavan added a commit that referenced this issue May 12, 2020
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well.

See also:
ai/nanoevents#44 (comment)

Fixes #444
@ctavan
Copy link
Member

ctavan commented May 13, 2020

For the time being the warning can simply be ignored for uuid, the functionality is not affected!

I think it might make more sense to actually fix this in react-native instead of adding this workaround to every single module on npm. If you agree, feel free to give your 👍 for react-native-community/cli#1168

@ljharb
Copy link

ljharb commented May 18, 2020

In general, I think it's a good idea for packages to explicitly add "./package.json": "./package.json" to their "exports" field.

However, unless the ecosystem is consistently willing to do this, tooling will have to determine a different mechanism for accessing package.jsons - something like findPackageJSONFrom(path.dirname(require.resolve('pkg'))).

@ctavan
Copy link
Member

ctavan commented May 18, 2020

To rephrase @ljharb's suggestion (assuming Node.js won't change the current behavior, see nodejs/node#33460):

  1. Packages that rely on meta information for other tools from their package.json must export it.
  2. Tools must be able to handle dependencies which don't export package.json gracefully (i.e. without crashing).
    • Tools which make use of meta information from package.json could warn if packages don't export package.json in order to help package maintainers decide if they need to export it or not.

ctavan added a commit that referenced this issue May 20, 2020
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well.

See also:
ai/nanoevents#44 (comment)

Fixes #444
ctavan added a commit that referenced this issue May 20, 2020
It appears that react-native and some bundlers rely on being able to
introspect the package.json file of a npm module. After adding the
`exports` field to package.json, only the paths defined in that field
are accessible to Node.js. In order to support introspection of the
package.json file itself it has to be listed as an export as well (see:
ai/nanoevents#44 (comment)).

It is currently being discussed to change Node.js' behavior again, see:
nodejs/node#33460
Until then, adding package.json to the exports field should result in
minimum breakage.

Fixes #444
@ctavan
Copy link
Member

ctavan commented May 20, 2020

Just release uuid@8.1.0 which now exports package.json, so the problem reported here should be gone for now.

@dmnsgn
Copy link

dmnsgn commented Jun 9, 2020

Hi @ctavan ,

I am seeing the same error using the latest version 8.1.0 but for the v4 exports:

Package subpath './v4' is not defined by "exports" in /Users/path-to-project/nested/in/node-modules-folder/node_modules/uuid/package.json

$ node -v
v14.4.0

RubenVerborgh pushed a commit to RubenVerborgh/AsyncIterator that referenced this issue Nov 3, 2021
Tools such as Components.js and react-native require introspection of
the package.json file. Since the new export syntax does not imply
package.json resolution yet (nodejs/node#33460), this has to be exported
explicitly. Ideally, this would be something that is done implicitly by
Node.js, but this is something that is still being discussed at
nodejs/node#33460. So until then, this has to be added explictly to
avoid breaking other tooling.

Related to uuidjs/uuid#444
ianprime0509 added a commit to ianprime0509/pitchy that referenced this issue Jul 15, 2022
Apparently, React Native and various bundler tools expect to be able to
import/require package.json, which will fail if `exports` is being used
but not exporting package.json:
uuidjs/uuid#444

Fixes #107
ianprime0509 added a commit to ianprime0509/pitchy that referenced this issue Jul 15, 2022
Apparently, React Native and various bundler tools expect to be able to
import/require package.json, which will fail if `exports` is being used
but not exporting package.json:
uuidjs/uuid#444

Fixes #107
tastydev added a commit to tastydev/pretty-bytes that referenced this issue Nov 30, 2022
If you are using bundlers like metro in rn-native (or tools that make use of metainformation of packages) you will recieve an import warning like:
```
warn Package pretty-bytes has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in /Users/****/Applications/****/****/app/node_modules/pretty-bytes/package.json
```

There was a similar Issue in `uuidjs`, for discussion you can see this issue uuidjs/uuid#444 (comment)
xbucks pushed a commit to xbucks/react-responsive that referenced this issue Oct 14, 2023
Apparently, React Native and various bundler tools expect to be able to
import/require package.json, which will fail if `exports` is being used
but not exporting package.json:
uuidjs/uuid#444

Fixes #107
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.

10 participants