Cli: Doesn't build after project initialization

Created on 15 Oct 2019  ยท  54Comments  ยท  Source: react-native-community/cli

After recent update in cli-platform-android the project doesn't start. Stating Cannot invoke method getErrorStream() on null object. I wanted to submit an issue for that package but I could not find a github repo for that.

$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 863 file(s) to forward-jetify. Using 8 workers...
info Starting JS server...
info Installing the app...
:ReactNative:Cannot run program "npx": CreateProcess error=2, โ•ั… ั”ั„ั€ั…ะ„ั‘ย  ัั€ั‰ะ„ัˆ ั”ัŠั€ั‡ั€ััโˆšั‰ ะ‡ั€ั‰ั‹
:ReactNative:Automatic import of native modules failed.

FAILURE: Build failed with an exception.

* Where:
Script 'C:\TEMP\natapp\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' line: 169

* What went wrong:
A problem occurred evaluating script.
> Cannot invoke method getErrorStream() on null object

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --v

erbose flag for more details.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where:
Script 'C:\TEMP\natapp\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' line: 169

* What went wrong:
A problem occurred evaluating script.
> Cannot invoke method getErrorStream() on null object

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

    at checkExecSyncError (child_process.js:621:11)
    at execFileSync (child_process.js:639:15)
    at runOnAllDevices (C:\TEMP\natapp\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:94:39)
    at buildAndRun (C:\TEMP\natapp\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\index.js:158:41)
    at C:\TEMP\natapp\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\index.js:125:12
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async Command.handleAction (C:\TEMP\natapp\node_modules\@react-native-community\cli\build\index.js:164:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm running on Windows 10 x64 and node version is 12.8.0

bug windows

Most helpful comment

I'm on same status
Node Version: v12.12.0
react-native --version
3.3.0-alpha.6

On Windows, npx command is "npx.cmd" and windows terminal can omit extensions for executables(i.e. bat,cmd,exe ) .so we can run npx by "npx" in the terminal, but it's looks only in terminal (except for *.exe)
I modified native_modules.gradle L.153 "npx --quiet react-native config" to "npx.cmd --quiet react-native config" ,then I could run run-android
but it's not smart and not portable.

All 54 comments

The code is here in this repo :) https://github.com/react-native-community/cli/tree/master/packages/platform-android. Fixing it is appreciated

I see Cannot run program "npx" in the error message. Can you verify npx works for you as a standalone command? It should ship with npm by default.

I see Cannot run program "npx" in the error message. Can you verify npx works for you as a standalone command? It should ship with npm by default.

Yes it does. I created the project using this command

npx react-native init MyApp --template react-native-template-typescript

And I noticed that platform-android was updated recently. Before this update everything worked fine.

Ww change quite a bit there to improve monorepo support. We would appreciate further debugging on this

Funny as I have another project inside a monorepo. And obviously I had some issues there due to lack of symlinks support and I thought that I eventually found the solution. Reinstalled the dependencies and it stopped working because of this issue.

Also tried this command
npx react-native init MyApp --template [email protected]

It works just fine there

I just tried on this template and can't reproduce, so most likely it's Windows related. Can you run npx --quiet react-native config exactly? This is what we run under the hood. Maybe the --quiet flag is a problem?

I'm also having this issue on a fresh created project on Windows.

yarn react-native --version output: 3.0.0-alpha.6
node version: 12.10.0
yarn version: 1.19.0

I had started a project with npx react-native, but react-native was installed globally, I uninstalled it and everything is working fine now

I'm having this issue as well on a new project when i run
react-native run-android

Node version : 12.12.0
react-native-cli version: 2.0.1
react-native version: 0.61.2

I'm also facing this issue from morning today. Till yesterday it all went fine.
react-native run-android

Node version : 10.16.3
react-native-cli version: 2.0.1
react-native version: 0.61.2

I'm on same status
Node Version: v12.12.0
react-native --version
3.3.0-alpha.6

On Windows, npx command is "npx.cmd" and windows terminal can omit extensions for executables(i.e. bat,cmd,exe ) .so we can run npx by "npx" in the terminal, but it's looks only in terminal (except for *.exe)
I modified native_modules.gradle L.153 "npx --quiet react-native config" to "npx.cmd --quiet react-native config" ,then I could run run-android
but it's not smart and not portable.

@zrelyydereva - this worked for the time being. thanks for that.

It worked for me after I manually installed cli-platform-android

Same error on Android.

:ReactNative:Cannot run program "npx": CreateProcess error=2
...
Cannot invoke method getErrorStream() on null object

Anybody have some idea with this condition?

I wonder if there's any way to rollback to previous version when it was working. I also noticed that all react native community dependencies do not appear in package.json. it would be great to have them there for a possibility to lock the version which is actually working.

It worked for me after I manually installed cli-platform-android
whats the command to do that..

CLI is a direct dependency of react-native and we want it to keep it this way. You can use yarn resolutions in package.json to pin to previous version:

"resolutions": {
  "@react-native-community/cli": "3.0.0-alpha.2",
  "@react-native-community/cli-platform-android": "3.0.0-alpha.2",
  "@react-native-community/cli-platform-ios": "3.0.0-alpha.2",
  "@react-native-community/cli-tools": "2.8.3"
}

CLI is a direct dependency of react-native and we want it to keep it this way. You can use yarn resolutions in package.json to pin to previous version:

"resolutions": {
  "@react-native-community/cli": "3.0.0-alpha.2",
  "@react-native-community/cli-platform-android": "3.0.0-alpha.2",
  "@react-native-community/cli-platform-ios": "3.0.0-alpha.2",
  "@react-native-community/cli-platform-tools": "2.8.3"
}

@react-native-community/cli-platform-tools => @react-native-community/cli-tools

I've sent a PR with a possible fix: https://github.com/react-native-community/cli/pull/801 that @zrelyydereva suggested (thanks!). Can you make sure it works for you?

Released alpha.7 with a fix for that. Please update the @react-native-community/cli to the latest version and let us know :)

thanks for Release new version.
It looks like work well!

I'm on same status
Node Version: v12.12.0
react-native --version
3.3.0-alpha.6

On Windows, npx command is "npx.cmd" and windows terminal can omit extensions for executables(i.e. bat,cmd,exe ) .so we can run npx by "npx" in the terminal, but it's looks only in terminal (except for *.exe)
I modified native_modules.gradle L.153 "npx --quiet react-native config" to "npx.cmd --quiet react-native config" ,then I could run run-android
but it's not smart and not portable.

me tooo!!!!!!

I was having the same issue when creating a new project, what worked for me was to restart the watchman tool.
watchman shutdown-server

Im facing the same problem on Ubuntu. I realized the cause is that "npx --quiet react-native config" command from file 'cli-platform-android/native_modules.gradle' is not being executed in root project directory, causing the error: "Unable to find React Native files. Make sure "react-native" module is installed in your project dependencies...". Running the same command in root project directory via terminal, it working as expected. How the project root path is defined in gradle files?

@david-laurentino This message means that looking up from your android project directory, CLI didn't find any package.json with react-native dependency inside. Make sure you define it.

The file is defined:
{
"name": "gfit-ts",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint .",
"postinstall": "patch-package"
},
"dependencies": {
"react": "16.9.0",
"react-native": "0.61.2"
...
}
Running the command "npx react-native start" and then "npx react-native run-android" in terminal from root directory project it works as expected. But if I try run debug from IDE (Webstorm), it fails because in this case the command "npx --quiet react-native config" is not being executed in root project directory as it should. I would like to know how the project path is found by gradle. I would think it's a problem of Webstorm, but the project build fails in Android Studio too with the same error.

Can the project root directory be defined in any way?

Since 3.0.0-alpha.7, running npx from any directory inside the project should successfully find the path to React Native and fetch configuration (try running it from android directory for example). Maybe Webstorm or AndroidStudio don't inherit PATH from environment correctly? What is the error by the way? It would be really more helpful if you created a new issue with _detailed_ error messages, environment info and reproduction repo.

Since 3.0.0-alpha.7, running npx from any directory inside the project should successfully find the path to React Native and fetch configuration (try running it from android directory for example). Maybe Webstorm or AndroidStudio don't inherit PATH from environment correctly? What is the error by the way? It would be really more helpful if you created a new issue with _detailed_ error messages, environment info and reproduction repo.

In fact, running "npx --quiet react-native config" in any project directory successfully finds the path to React Native. But this command seems to being running in wrong directory when executed on the line 159 of file cli-platform-android/native-modules.gradle, causing the error .

It seems that the $rootDir variable used by gradle is incorrectly. As I understand it, this variable comes from the getRootDir() method of a class named Project

Please update the @react-native-community/cli to the latest version, there's no getRootDir in latest CLI used by RN 0.61.

Please update the @react-native-community/cli to the latest version, there's no getRootDir in latest CLI used by RN 0.61.

I have the alpha 7 version. I trying to reinstall react native cli. According react native documentation, I must run "npm install -g react-native-cli", but if i understood right, now should install the community cli, so I must running "npm install -g @react-native-community/cli" instead. Is is right?

Docs are not updated yet. Don't install anything globally, uninstall global react-native, react-native-cli or @react-native-community/cli if you have any.

Docs are not updated yet. Don't install anything globally, uninstall global react-native, react-native-cli or @react-native-community/cli if you have any.

The solution was use react-native version "0.60.5" and @react-native-community/cli version 2.9.0. Use the last version of react-native (0.61.2) and the last version of cli (3.0.0-alpha.7) causes the automatic import of native modules fails due the command "npx --quiet react-native config" is being running in wrong directory instead of project directory. It occurs in method located in native_modules.gradle: getReactNativeConfig()

Docs are not updated yet. Don't install anything globally, uninstall global react-native, react-native-cli or @react-native-community/cli if you have any.

The solution was use react-native version "0.60.5" and @react-native-community/cli version 2.9.0. Use the last version of react-native (0.61.2) and the last version of cli (3.0.0-alpha.7) causes the automatic import of native modules fails due the command "npx --quiet react-native config" is being running in wrong directory instead of project directory. It occurs in method located in native_modules.gradle: getReactNativeConfig()

I found another solution for now: After installing last version of cli, I replace the file cli-platform-android/native_modules.gradle for the previous version one(2.9.0). That way it works and I can use last version of react-native and cli. I noticed the most recent version of this file detects the root of project automatically, as line 226 states: "Passing custom root is deprecated. CLI detects root automatically now". Maybe it is not working in some scenarios. The old file has a method called getReactNativeProjectRoot() whose was removed in new version. The return of this method was passed as argument for cmdProcess = Runtime.getRuntime().exec(command, null, root) while "npx --quiet react-native config" was running. In the new version, this method is called without root param: "Runtime.getRuntime().exec(command)".The problem seems to be related to this.

Thank for your help. For now, I must use this weird solution.

@david-laurentino please provide a repro that we can download. Otherwise it's not actionable.

Docs are not updated yet. Don't install anything globally, uninstall global react-native, react-native-cli or @react-native-community/cli if you have any

I fount areplaced the file cli-platform-android/native_modules.gradle for the previous version one (2.9)

@david-laurentino please provide a repro that we can download. Otherwise it's not actionable.

It occurs with any project created with react native version 0.62.1 and typescript, for example:
npx react-native init TestApp --version [email protected] --template react-native-template-typescript on Ubuntu 18.04LTS
Omitting the version option, everything works as expected, but this way the project is create with RN version 0.60.5 and CLI version 2.9.0.

Had the same issue on linux.
Just installed npx to machine globally and everything worked :tada:
sudo npm install -g npx

@githuboftigran are you using Node version lower than 8? npx is bundled with Node by default since then.

I'm using 8.10, but npx wasn't installed globally.

Seems like a bug in Node. Please update it to latest LTS then (v12). It's going to be a tad faster as well.

@thymikee yep, will do, thanks!

The problem here is that $PATH variable isn't inherited through getRuntime().exec. I do not know exactly why, but seams to be related to the NVM.

I was running into this error: Unexpected empty result of running 'npx --quiet --no-install react-native config' command from 'null' directory. when building in appcenter (it built fine locally) and ended up having to downgrade cli-platform-android to ^2.9.0 based on this comment: https://github.com/facebook/react-native/issues/26929#issuecomment-555014695 In order to get it to work. I'm dealing with a monorepo and think it was a pathing issue, although it built fine in appcenter for iOS.

I had same issue as @Vednus. I ended up adding the react-native-cli versions as resolutions in the package.json as described above.

    "resolutions": {
        "@react-native-community/cli": "2.9.0",
        "@react-native-community/cli-platform-android": "2.9.0",
        "@react-native-community/cli-platform-ios": "2.9.0",
        "@react-native-community/cli-tools": "2.8.3"
    },

Had the same issue on linux.
Just installed npx to machine globally and everything worked
sudo npm install -g npx

it's working. ty

image
it seems we have the same problems ,by the way , my version : react-native-cli: 2.0.1
react-native: 0.61.5

Maybe the $PATH variable isn't inherited through Java Runtime correctly. The $PATH inherited is one statitically defined. If you use NVM that defines $PATH dinamically including node according version, it is not propagated to Java Runtime which is where commands are executed. Install npx globally solve the problem once It put the npx in $PATH statically. Here despite having NVM I put the node on $PATH statically, but theses solutions seems workaround.

Installing node/npx globally is not a good fix. E.g. test servers testing many branches/projects shouldn't rely on a shared node/npx version - different branches/projects will in general need different versions.

To support nodenv/nvm etc., npx (and node etc.) should be taken from the PATH.

macOS apps with a UI, e.g. Android Studio and the Terminal sometimes used to start the packager/bundler, generally don't have env. vars. set unfortunately.

Looks like npx causes more issues than we anticipated. We'll need to use a different approach and once that's done, we'll as for feedback and more thorough testing

Caused by: java.io.IOException: Cannot run program "npx.cmd": CreateProcess error=2, ็ณป็ปŸๆ‰พไธๅˆฐๆŒ‡ๅฎš็š„ๆ–‡ไปถใ€‚
at java_lang_Runtime$exec$0.call(Unknown Source)
at ReactNativeModules.getCommandOutput(F:\RNProject\AwesomeProject\node_modules\@react-native-communitycli-platform-android\native_modules.gradle:154)
at ReactNativeModules.getCommandOutput(F:\RNProject\AwesomeProject\node_modules\@react-native-communitycli-platform-android\native_modules.gradle)
at ReactNativeModules.getReactNativeConfig(F:\RNProject\AwesomeProject\node_modules\@react-native-communitycli-platform-android\native_modules.gradle:194)
at ReactNativeModules$getReactNativeConfig.callCurrent(Unknown Source)
at ReactNativeModules.(F:\RNProject\AwesomeProject\node_modules\@react-native-communitycli-platform-android\native_modules.gradle:84)
at native_modules_d6l6y8msnqttbqz64d6riqzsh.run(F:\RNProject\AwesomeProject\node_modules\@react-native-communitycli-platform-android\native_modules.gradle:238
)
at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:90)
... 147 more
Caused by: java.io.IOException: CreateProcess error=2, ็ณป็ปŸๆ‰พไธๅˆฐๆŒ‡ๅฎš็š„ๆ–‡ไปถใ€‚
... 155 more

NVM sets up it's environment variables in the .bashrc file, which only sets the environment variables for Bash. These seem to not be accessible to terminal commands ran from Gradle. The solution is to move the NVM specific code from the .bashrc file in the home directory to the .profile file, also in the home directory. This NVM specific code looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Restart your computer after, and try to open Android Studio again.

Same exact problem here. Running npx command in a monorepo and the directory is null.
First I get:
:ReactNative:Unexpected empty result of running 'npx.cmd --quiet --no-install react-native config' command from 'null' directory.
:ReactNative:Running 'npx.cmd --quiet --no-install react-native config' command from 'null' directory failed

Then , decommenting the dir argument at line 194of native_modules.gradle the error disappears but still crashed during build:

What went wrong:
A problem occurred evaluating script.
React Native CLI failed to determine Android project configuration. This is likely due to misconfiguration. Config output:

UPDATE: I downgraded react-native to 0.60.0 and was able to see the build progress. Unfortunately when it got to Task :app:processDebugResources it FAILED.

Android resource linking failed

In my case npx was not found I think that this is because I installed node using NVM

NVM sets up it's environment variables in the .bashrc file, which only sets the environment variables for Bash. These seem to not be accessible to terminal commands ran from Gradle. The solution is to move the NVM specific code from the .bashrc file in the home directory to the .profile file, also in the home directory. This NVM specific code looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Restart your computer after, and try to open Android Studio again.

In my case through the terminal I was able to compile and run it, I just couldn't sync through Android Studio.
I use linux and my Android Studio was installed via flatpak.
I performed the manual installation of Android Studio and gradlew got access to npx

Was this page helpful?
0 / 5 - 0 ratings