Cli: Cannot get property 'packageName' on null object

Created on 1 Sep 2019  路  25Comments  路  Source: react-native-community/cli

Moving from #537

Environment

System:
OS: macOS 10.15
CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Memory: 2.70 GB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.8.0 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/node
Yarn: 1.17.3 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/yarn
npm: 6.10.3 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3
Android SDK:
API Levels: 28
Build Tools: 28.0.3
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5791312
Xcode: 10.3/10G8 - /usr/bin/xcodebuild

Description

Can't get react-native 0.61 to work on a monorepo on Android. Build fails with the error: Cannot get property 'packageName' on null object. iOS works as expected.

Running yarn react-native config returns this result:

{
  "root": "/Users/user/Projects/react-native-web-monorepo",
  "reactNativePath": "/Users/user/Projects/react-native-web-monorepo/node_modules/react-native",
  "dependencies": {},
  "commands": [],
  "assets": [],
  "platforms": {},
  "haste": {
    "providesModuleNodeModules": [],
    "platforms": []
  },
  "project": {}
}

Using nohoist or installing react-native inside packages/mobile instead of the root is not an option because that causes a range of other issues.

Reproducible Demo

Branch rn61 with error: https://github.com/brunolemos/react-native-web-monorepo/tree/rn61
Branch master working, with workarounds: https://github.com/brunolemos/react-native-web-monorepo

bug

Most helpful comment

@brunolemos

I did have to add react-native dependency to the root package.json and create a react-native.config.js though. Not a big deal but hopefully that gets fixed, this workaround was not necessary on <= 0.59.

I have tried your repository and I don't think this is a correct way to run things.

The problem that appears here is that you have set sourceDir to be <root>/packages/mobile/android.

The CWD is: <root>/packages/mobile at the time of running your script (via yarn workspace mobile).

As a result, React Native CLI tries to look for Android files inside <root>/packages/mobile/packages/mobile/android, which produces null and as a result, makes Android files check for packageName of null.

Right now, the paths are relative to the CWD. Maybe they should be relative to the location of the configuration file, but that would be a change.

Moving from #537

Environment

System:
OS: macOS 10.15
CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Memory: 2.70 GB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.8.0 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/node
Yarn: 1.17.3 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/yarn
npm: 6.10.3 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3
Android SDK:
API Levels: 28
Build Tools: 28.0.3
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5791312
Xcode: 10.3/10G8 - /usr/bin/xcodebuild

Description

Can't get react-native 0.61 to work on a monorepo on Android. Build fails with the error: Cannot get property 'packageName' on null object. iOS works as expected.

Running yarn react-native config returns this result:

{
  "root": "/Users/user/Projects/react-native-web-monorepo",
  "reactNativePath": "/Users/user/Projects/react-native-web-monorepo/node_modules/react-native",
  "dependencies": {},
  "commands": [],
  "assets": [],
  "platforms": {},
  "haste": {
    "providesModuleNodeModules": [],
    "platforms": []
  },
  "project": {}
}

Using nohoist or installing react-native inside packages/mobile instead of the root is not an option because that causes a range of other issues.

Reproducible Demo

Branch rn61 with error: https://github.com/brunolemos/react-native-web-monorepo/tree/rn61
Branch master working, with workarounds: https://github.com/brunolemos/react-native-web-monorepo

I modify node_modules/@react-native-community/cli-platform-android/native_modules.gradle
at line 157
cmdProcess = Runtime.getRuntime().exec(command)
change to
cmdProcess = Runtime.getRuntime().exec(command,null,new File(System.getProperty("user.dir")).getParentFile())
work OK

All 25 comments

I am also facing the same problem though my react-native version is 0.59, I couldn't fix this issue. If you have any optional idea please share me.

I met the same problem and I got it to work by adding a file react-native.config.js to describe my custom android project info.
check this document.

Is it possible to upgrade to react-native 0.61 on a monorepo while this is not fixed? Any older version of @react-native-community/cli I can use that would work? I'm holding off upgrading just because of this (still at 0.59).

We have exactly the same problem for 0.60.5. Same project structure setup. We've got this error when we set autolinking as it's described here.

Any idea what to do?

I was able to workaround this issue by disabling auto linking. Here's the commit diff.

But I'm still stuck at 0.59 because when trying to upgrade to 0.60.5 I get this error and to 0.61 I get something similar to this error.

Successfully upgraded to [email protected] here: https://github.com/brunolemos/react-native-web-monorepo (master)

I did have to add react-native dependency to the root package.json and create a react-native.config.js though. Not a big deal but hopefully that get fixed, this workaround was not necessary on <= 0.59.

@brunolemos is auto linking working for you on this new version?

@joaovpmamede haven't tried yet, please fork and try: https://github.com/brunolemos/react-native-web-monorepo

Getting this as well.

Auto link didn't work for me.

Having same issue with an example app of a package which contains no native modules.

E: Adding this to react-native.config.js resolved the issue:

    project: {
        ios: {
            project: 'example/ios/Example.xcodeproj'
        },
        android: {
            sourceDir: 'example/android'
        }
    },

@brunolemos

I did have to add react-native dependency to the root package.json and create a react-native.config.js though. Not a big deal but hopefully that gets fixed, this workaround was not necessary on <= 0.59.

I have tried your repository and I don't think this is a correct way to run things.

The problem that appears here is that you have set sourceDir to be <root>/packages/mobile/android.

The CWD is: <root>/packages/mobile at the time of running your script (via yarn workspace mobile).

As a result, React Native CLI tries to look for Android files inside <root>/packages/mobile/packages/mobile/android, which produces null and as a result, makes Android files check for packageName of null.

Right now, the paths are relative to the CWD. Maybe they should be relative to the location of the configuration file, but that would be a change.

One thing we could do here is to resolve all paths provided under project/dependency relative to the location of the configuration so that at the time of reading the configuration they are absolute.

Thoughts?

is auto linking working for you on this new version?
Auto link didn't work for me.

Auto linking did work for me on ios, but not on android.

The problem that appears here is that you have set sourceDir to be /packages/mobile/android.

The only reason I added this sourceDir was to fix the Cannot get property 'packageName' on null object error, so I wouldn't say it is the cause, but the workaround.

@brunolemos

I did have to add react-native dependency to the root package.json and create a react-native.config.js though. Not a big deal but hopefully that gets fixed, this workaround was not necessary on <= 0.59.

I have tried your repository and I don't think this is a correct way to run things.

The problem that appears here is that you have set sourceDir to be <root>/packages/mobile/android.

The CWD is: <root>/packages/mobile at the time of running your script (via yarn workspace mobile).

As a result, React Native CLI tries to look for Android files inside <root>/packages/mobile/packages/mobile/android, which produces null and as a result, makes Android files check for packageName of null.

Right now, the paths are relative to the CWD. Maybe they should be relative to the location of the configuration file, but that would be a change.

Moving from #537

Environment

System:
OS: macOS 10.15
CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Memory: 2.70 GB / 16.00 GB
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 12.8.0 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/node
Yarn: 1.17.3 - /var/folders/xh/gcxkb1qx2rd3fmqz5vxrs2lh0000gn/T/yarn--1567321836628-0.386672585258681/yarn
npm: 6.10.3 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3
Android SDK:
API Levels: 28
Build Tools: 28.0.3
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5791312
Xcode: 10.3/10G8 - /usr/bin/xcodebuild

Description

Can't get react-native 0.61 to work on a monorepo on Android. Build fails with the error: Cannot get property 'packageName' on null object. iOS works as expected.

Running yarn react-native config returns this result:

{
  "root": "/Users/user/Projects/react-native-web-monorepo",
  "reactNativePath": "/Users/user/Projects/react-native-web-monorepo/node_modules/react-native",
  "dependencies": {},
  "commands": [],
  "assets": [],
  "platforms": {},
  "haste": {
    "providesModuleNodeModules": [],
    "platforms": []
  },
  "project": {}
}

Using nohoist or installing react-native inside packages/mobile instead of the root is not an option because that causes a range of other issues.

Reproducible Demo

Branch rn61 with error: https://github.com/brunolemos/react-native-web-monorepo/tree/rn61
Branch master working, with workarounds: https://github.com/brunolemos/react-native-web-monorepo

I modify node_modules/@react-native-community/cli-platform-android/native_modules.gradle
at line 157
cmdProcess = Runtime.getRuntime().exec(command)
change to
cmdProcess = Runtime.getRuntime().exec(command,null,new File(System.getProperty("user.dir")).getParentFile())
work OK

@mllw thank you!!!

@gaozhaoqun did you find solution ? if yes then share pls!

@detroit007 The solution is in the last comment by @gaozhaoqun. Thanks @gaozhaoqun
Go to the node_modules/@react-native-community/cli-platform-android/native_modules.gradle
at line 157 (or find cmdProcess)
Is should be something like this:
cmdProcess = Runtime.getRuntime().exec(command)
Change it to
cmdProcess = Runtime.getRuntime().exec(command,null,new File(System.getProperty("user.dir")).getParentFile())

@mllw suggestion worked for me!

I don't think it's a good idea to change any code that is inside node_modules folder. Since it's added via npm and ignored by git any changes you will make end up staying only on your local.

However, there's another solution that's mentioned in one of the recently opened issues. See https://github.com/react-native-community/cli/issues/835#issuecomment-549134018

Basically, add cli-platform-android package to your dev dependencies:

npm install @react-native-community/cli-platform-android --save-dev

This solved my problem and seems safer. @mllw @gaozhaoqun @detroit007 @chawlatarika5 @johnshroff

I'm also having this problem - @chawlatarika5's solution is working for me but obviously isn't ideal.

Some coworkers are able to run the same project with no issues, without editing node_modules. We can't spot any difference in environment - we're stumped!

@chawlatarika5's solution does not work for me, as I still get the exact same error.
Changing @react-native-community/cli-platform-android to v2.9.0 and patching it like this, seems to kind of work.

-    def command = "node ./node_modules/react-native/cli.js config"
+    def command = "../../node_modules/react-native/cli.js config"

But with this, I need to run gradlew clean every time, or I get other errors.

I was having a similar problem with this error message and then found that it was caused by a syntax error in my package.json (a missing comma 馃槥), so it is worth verifying that file is correctly formatted if it has been changed recently

To everyone complaining it is a bad idea to modify content of @react-native-community/cli-platform-android, you can use patch-package and run it after npm or yarn install, without worrying your modifications to be overwritten.

  1. Modify cli-platform-android
  2. Run npx patch-package @react-native-community/cli-platform-android
  3. Add this to your package.json, under scripts.
    postinstall: "npx patch-package

I am still getting this error running react-native 0.63.4

Execution failed for task ':app:generateDebugBuildConfig'.
> Failed to calculate the value of task ':app:generateDebugBuildConfig' property 'buildConfigPackageName'.
   > Failed to query the value of property 'packageName'.
      > java.lang.NumberFormatException: For input string: "1.0.0"
Was this page helpful?
0 / 5 - 0 ratings