question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Package subpath './package.json' is not defined by "exports"

See original GitHub issue

Environment

System:
    OS: macOS 10.15.4
    CPU: (4) x64 Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
    Memory: 94.37 MB / 16.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 14.2.0 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/local/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.4, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 11.4.1/11E503a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_121 - /usr/bin/javac
    Python: 3.7.7 - /usr/local/opt/python/libexec/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.13.1 => 16.13.1
    react-native: 0.62.2 => 0.62.2
  npmGlobalPackages:
    *react-native*: Not Found

Description

When running react-native under Node.js 14.x on a project that uses npm modules which specify the new exports field in package.json, react-native will print the following warning (in this example the uuid package is used):

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

The same issue has been reported in other places:

The reason is that, starting in Node.js 14.x, as soon as an npm module defines the exports field in package.json, only the files listed there are exported. If package.json is not included in that list, it’s no longer possible to do things like require.resolve('uuid/package.json');.

The problematic piece of code is: https://github.com/react-native-community/cli/blob/5819a17d148726a1185420b6df345dad6c81c81b/packages/cli/src/tools/config/resolveNodeModuleDir.ts#L11-L13

For a package like uuid which does not contain react-native specific configuration in package.json this is not a problem, but for modules that do contain such config in package.json they will be forced to add package.json to their exports field.

An alternative would be to not rely on require.resolve and instead use https://github.com/browserify/resolve

As far as I know this is how rollup.js solves this issue, see https://github.com/rollup/plugins/issues/208#issuecomment-614319271

What is the maintainers take on this? Do you think that every single module on npm that starts making use of the exports field should include package.json in the exports? Or could this be fixed in react-native?

Reproducible Demo

  1. Use Node.js 14.x
  2. npm i uuid@8.0.0
  3. Open noderepl:
> require.resolve('uuid/package.json');
Uncaught:
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in /PROJECT/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.resolve (internal/modules/cjs/helpers.js:78:19)
    at repl:1:9
    at Script.runInThisContext (vm.js:131:20)
    at REPLServer.defaultEval (repl.js:436:29)
    at bound (domain.js:429:14)
    at REPLServer.runBound [as eval] (domain.js:442:12) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:51
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

11reactions
mificommented, Sep 11, 2021

What’s the consequence of the warning? Does it mean that the package in question will not be included in the bundle?

8reactions
sindresorhuscommented, Jul 22, 2022

Please don’t open issues/PRs on open source projects about this. It is not expected that packages export their package.json file. This is a problem React Native needs to fix, not every single package out there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

package.json is not defined by "exports" · Issue #444 - GitHub
package.json' is not defined by "exports" when upgrading from v7.0.3 to v8.0.0 ... Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath '.
Read more >
Package subpath is not defined by "exports" - Stack Overflow
You problem sound like a dependance problem. First, try to delete node_modules directory and issue a npm install in order to re import ......
Read more >
Package subpath 'v4' is not defined by 'exports' | bobbyhadz
The error '[ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './v4' is not defined by "exports"' occurs because the uuid module removed the option to import from ...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
When the "exports" field is defined, all subpaths of the package are encapsulated and no longer available to importers. For example, require('pkg/subpath.js') ...
Read more >
package subpath is not defined by exports in - You.com
Delete the exports field in node_modules/cesium/package.json . Re-launch npm run dev . Open side panel.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found