long.js icon indicating copy to clipboard operation
long.js copied to clipboard

"./package.json" is not defined by "exports" warning in React Native

Open leonardogbxv opened this issue 2 years ago • 2 comments

Description

A warning alert from long.js package is being outputed on command line when running "react-native run-android" on React Native.

Output

This is the command line output.

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

Additional information

"long": "5.2.3"
"react-native": "0.64.4"
"react": "16.13.1"
"node": "16.17.0"

leonardogbxv avatar Aug 24 '23 14:08 leonardogbxv

Similar problem happening here.

The long.js functionalities doesn't seems to be affected, but this warning can be quite annoying.

leonardogbxv avatar Aug 24 '23 14:08 leonardogbxv

A workaround for now. You can run it on the "postinstall" of your package.json project.

function fixLongExportWarning() {
  const packagePath = 'node_modules/long/package.json';

  fs.readFile(packagePath, 'utf8', (readError, data) => {
    if (readError) {
      return console.error("['fixLongExportWarning' Read Error]", readError);
    }

    try {
      const packageData = JSON.parse(data);

      // add new field
      packageData.exports['./package.json'] = './package.json';

      // replace current package.json
      fs.writeFile(packagePath, JSON.stringify(packageData, null, 2), 'utf8', (writeError) => {
        if (writeError) {
          console.error("['fixLongExportWarning' Write Error]", writeError);
        }
      });
    } catch (error) {
      console.error("['fixLongExportWarning' Error]", error);
    }
  });
}

leonardogbxv avatar Aug 24 '23 14:08 leonardogbxv