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 subpath './package.json' is not defined by "exports" #1168

Closed
ctavan opened this issue May 13, 2020 · 9 comments · Fixed by lint-staged/lint-staged#1059 or #1655 · May be fixed by thekelvinliu/country-code-emoji#23
Closed

Comments

@ctavan
Copy link

ctavan commented May 13, 2020

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:

require.resolve(path.join(packageName, 'package.json'), {
paths: [root],
}),

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 rollup/plugins#208 (comment)

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'
}
@farwayer
Copy link
Contributor

farwayer commented May 13, 2020

From nodejs docs:

Now only the defined subpath in "exports" can be imported by a consumer

So package.json should be defined in exports to be available for import. But i can guess many package authors will not include it in exports because in most situations importing package.json is not necessary. So it should be processed on react-native-cli side. One of possible solution is read and parse package.json file instead importing it. This is better than adding extra browserify/resolve package as you suggested.

@ctavan
Copy link
Author

ctavan commented May 13, 2020

Just to clarify: The problem in react-native cli is not the loading of the package.json file itself, that happens through cosmiconfig:

export function readConfigFromDisk(rootFolder: string): UserConfig {
const explorer = cosmiconfig('react-native', {
searchPlaces,
stopDir: rootFolder,
});
const searchResult = explorer.searchSync(rootFolder);

which uses fs.readFile() under the hood: https://github.com/davidtheclark/cosmiconfig/blob/d94bd6cd776b839c375d10846f97913b3d4e5763/src/readFile.ts#L3-L8

The problem is about resolving the path to the module's package.json file which happens here:

export default function resolveNodeModuleDir(
root: string,
packageName: string,
): string {
return path.dirname(
require.resolve(path.join(packageName, 'package.json'), {
paths: [root],
}),
);
}

@elsurudo
Copy link

elsurudo commented May 2, 2021

Is there a reasonable workaround without needing to make a PR to every project with this issue?

Morita0711 added a commit to Morita0711/npm-zod that referenced this issue Jul 3, 2022
When using Zod in a react-native app, we see the following warning when running the bundler:
```
warn Package zod has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in <path>/node_modules/zod/package.json
```

The problem is described in react-native-community/cli#1168 and that is where it should be properly fixed. In the meantime (that issue has been open for some time), the warning can be fixed in affected packages by adding an export for `package.json`.
@hengkx
Copy link

hengkx commented Jul 21, 2022

 Invalid `Podfile` file: 767: unexpected token at 'Error: Cannot find module 'xxxxx/node_modules/typeorm/package.json.js'

    at createEsmNotFoundErr (node:internal/modules/cjs/loader:960:15)

    at finalizeEsmResolution (node:internal/modules/cjs/loader:953:15)

    at resolveExports (node:internal/modules/cjs/loader:482:14)

    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)

    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)

    at Function.resolve (node:internal/modules/cjs/helpers:108:19)

    at resolveNodeModuleDir (xxxxx/node_modules/@react-native-community/cli-tools/build/resolveNodeModuleDir.js:24:42)

    at xxxxx/node_modules/@react-native-community/cli-config/build/loadConfig.js:93:76

    at Array.reduce (<anonymous>)

    at loadConfig (xxxxx/node_modules/@react-native-community/cli-config/build/loadConfig.js:91:134)

JameelJiwani added a commit to JameelJiwani/country-code-emoji that referenced this issue Jul 21, 2022
When sub-modules contain multiple exports in react-native projects, the root package.json is required


Resolves: react-native-community/cli#1168
@sindresorhus
Copy link

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.

jedahan added a commit to jedahan/d3-array that referenced this issue Jul 25, 2022
React Native community cli parses package.json to determine the local filesystem path of imported modules.

See react-native-community/cli#1168
jedahan added a commit to jedahan/d3-scale that referenced this issue Jul 25, 2022
@react-native-community/cli (used by expo and I *think* react native), parses package.json to determine package paths.

This is necessary to build react native projects. See react-native-community/cli#1168 for more details.
jedahan added a commit to jedahan/d3-interpolate that referenced this issue Jul 25, 2022
@react-native-community/cli (used by expo and I think react native), parses package.json to determine package paths.

This is necessary to build react native projects. See react-native-community/cli#1168 for more details.
StarDev930 pushed a commit to StarDev930/zod that referenced this issue Jan 29, 2023
When using Zod in a react-native app, we see the following warning when running the bundler:
```
warn Package zod has been ignored because it contains invalid configuration. Reason: Package subpath './package.json' is not defined by "exports" in <path>/node_modules/zod/package.json
```

The problem is described in react-native-community/cli#1168 and that is where it should be properly fixed. In the meantime (that issue has been open for some time), the warning can be fixed in affected packages by adding an export for `package.json`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment