Cli: Autolinking fails on Windows

Created on 4 Jul 2019  ·  80Comments  ·  Source: react-native-community/cli

Environment

System:
    OS: Windows 10
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 9.61 GB / 15.89 GB
  Binaries:
    Node: 10.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.16.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
  SDKs:
    Android SDK:
      Android NDK: 18.1.5063045
  IDEs:
    Android Studio: Version  3.3.0.0 AI-182.5107.16.33.5264788

Output of react-native --version:

react-native-cli: 2.0.1
react-native: 0.60.0

Description

This is the error I am getting while running react-native run-android or .\gradlew app:installDebug:

:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "C:\Users\thecodrr\Source\Repos\Mysue\android\.."): CreateProcess error=2, The system cannot find the file specified
:ReactNative:Automatic import of native modules failed.

> Configure project :app
:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "C:\Users\thecodrr\Source\Repos\Mysue\android\.."): CreateProcess error=2, The system cannot find the file specified
:ReactNative:Automatic import of native modules failed.

I tried running the gradlew app:installDebug command on Git Bash but still got the same error.

Reproducible Demo

  1. Create a new project using react-native init
  2. Run yarn add react-native-track-player@latest
  3. Run yarn react-native run-android
bug windows

Most helpful comment

Let's get some facts straight:

  1. @react-native-community/cli is a dependency of react-native. That makes it a "transitive dependency" of your project.
  2. react-native-cli is a global package, we discourage to use it, it's kind of a proxy to @react-native-community/cli, but it's a bit more complicated that that.
  3. We update the CLI independently of React Native itself.

Now that we know the facts:

Solution is to update your CLI version:

  1. If you use lock files (_yarn.lock_, _package-lock.json_) - find the @react-native-community/cli entry, remove it, run yarn install/npm install once again

    1. If you don't use lock files – you should. But in such case, remove node_modules and run npm install again.

  2. run npm list @react-native-community/cli or yarn list @react-native-community/cli and verify you're on the latest version (above 2.1.0).

After performing these steps you should be on the latest CLI version. Feel free to do it once in a while, because we release often.

All 80 comments

Hey! Thanks for filing this issue. Unfortunately we don't have Windows machines to test on. We used to do node ./node_modules/.bin/react-native config, but we got a report that it doesn't work on Windows, but ./node_modules/.bin/react-native config works. So could you share some more information about that?

I confirm that ./node_modules/.bin/react-native config works on Windows 10 and node ./node_modules/.bin/react-native config doesn't.

It's already resolved and fixed in @react-native-community/[email protected], make sure you have it installed (check the lock file)

I already have the version 2.0.2 installed:

"@react-native-community/cli@^2.0.1":
  version "2.0.2"
  resolved "https://registry.yarnpkg.com/@react-native-community/cli/-/cli-2.0.2.tgz#a157722d2a506e7e9a7d1079e3032b18ef6b0378"
  integrity sha512-U0v8QuFYnX9AvUOew9LfJS9FgnDtYsz7HhbsRyvXtaFu4kUnq2g7MgkC4zG0y3oqQzlOxhcO7Hoyv80XDsBQoQ==

Then you have a caching issue :) check your node modules, the updated code is there

I deleted the node_modules folder and run yarn again. I confirm that @react-native-community/[email protected] was installed correctly, but the error still persists:

> Configure project :app:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "C:\Users\vbastos\Desktop\chundaApp\android\.."): 
CreateProcess error=2, O sistema nÒo pode encontrar o arquivo especificado
:ReactNative:Automatic import of native modules failed.

Okay so doing node ./node_modules/.bin/react-native config gives this:

* Where:
Script 'C:\Users\thecodrr\Source\Repos\Mysue\node_modules\@react-native-community\cli-platform-android\native_modules.gradle' line: 182

* What went wrong:
A problem occurred evaluating settings 'Mysue'.
> Text must not be null or empty

And doing node ./node_modules/react-native/cli.js config stops the gradle from proceeding, stucking on this:

<-------------> 0% INITIALIZING [7s]
> Evaluating settings 

In the second case, the node process never exits so cmdProcess.waitFor() keeps waiting and waiting. In the first case, the command only inits the node without executing the command.

Adding a delay of, let's say 5 seconds, with cmdProcess.waitFor(5000, java.util.concurrent.TimeUnit.MILLISECONDS) and command node ./node_modules/react-native/cli.js config executes without a hitch albeit after waiting 5 seconds to get config.

@thymikee I upgraded to 2.0.2 but the error still persists. I have this command ./node_modules/.bin/react-native config in my native_modules.gradle file.

@thymikee after deleting node_modules folder, doing yarn cache clean the problem is still there. I tried on Git Bash, Powershell and CMD. And version is 2.0.2. So can we please reopen this?

Sure. But you'll need to help us make it work with your environment. We got signals that it works on some Windows, but looks like not all of them. Help appreciated :)

Also check if running this command alone, run from terminal, produces a json output. Maybe it takes too long

same here, i will retry in MacOS

"@react-native-community/cli-tools@^2.0.2": version "2.0.2" resolved "https://registry.yarnpkg.com/@react-native-community/cli-tools/-/cli-tools-2.0.2.tgz#1dc4055f27f1b2fe3d0493959a6fc20467e93fe0" integrity sha512-6OOKrE1Sdq1Lmcdp2K68J5PsG5G80a9USa9I1Kv92wvPHUup6IRt+Dy7E8IZqxmskzC/mlOR62Oh1CB3sFm84g== dependencies: chalk "^1.1.1" lodash "^4.17.5" mime "^2.4.1" node-fetch "^2.5.0"

there is still an error

same here

Windows 10, same error even after node ./node_modules/react-native/cli.js config (got a big json), and ./node_modules/react-native config which got a small json.

And the I am sure the cli version is 2.0.2.

Error:

The system cannot find the file specified, ... Automatic import of native modules failed

...
info Installing the app...
:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "E:\Projects\zgyt\drpapp\android\.."): CreateProcess error=2, 系统找不到指定的文
件。nnecting to Daemon
:ReactNative:Automatic import of native modules failed.

> Configure project :app
:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "E:\Projects\zgyt\drpapp\android\.."): CreateProcess error=2, 系统找不到指定的文
件。
:ReactNative:Automatic import of native modules failed.

> Task :app:installDebug
...

@thymikee Running ./node_modules/.bin/react-native config from the root directory of the projects directly using Powershell gives the correct output.

I have currently changed my native_modules.gradle's getReactNativeConfig() to this and it works correctly:

ArrayList<HashMap<String, String>> getReactNativeConfig() {
    if (this.reactNativeModules != null) return this.reactNativeModules
    ArrayList<HashMap<String, String>> reactNativeModules = new ArrayList<HashMap<String, String>>()

    def cmdProcess
    def root = getReactNativeProjectRoot()
    def command = "powershell ./node_modules/.bin/react-native config"

    try {
      cmdProcess = Runtime.getRuntime().exec(command, null, root)
      cmdProcess.waitFor(5, java.util.concurrent.TimeUnit.SECONDS)
    } catch (Exception exception) {
      this.logger.warn("${LOG_PREFIX}${exception.message}")
      this.logger.warn("${LOG_PREFIX}Automatic import of native modules failed.")
      return reactNativeModules
    }

    def reactNativeConfigOutput = cmdProcess.in.text
    def json = new JsonSlurper().parseText(reactNativeConfigOutput)
    this.packageName = json["project"]["android"]["packageName"]
    def dependencies = json["dependencies"]

    dependencies.each { name, value ->
      def platformsConfig = value["platforms"];
      def androidConfig = platformsConfig["android"]

      if (androidConfig != null && androidConfig["sourceDir"] != null) {
        this.logger.info("${LOG_PREFIX}Automatically adding native module '${name}'")

        HashMap reactNativeModuleConfig = new HashMap<String, String>()
        reactNativeModuleConfig.put("name", name)
        reactNativeModuleConfig.put("nameCleansed", name.replaceAll('/', '_'))
        reactNativeModuleConfig.put("androidSourceDir", androidConfig["sourceDir"])
        reactNativeModuleConfig.put("packageInstance", androidConfig["packageInstance"])
        reactNativeModuleConfig.put("packageImportPath", androidConfig["packageImportPath"])
        this.logger.trace("${LOG_PREFIX}'${name}': ${reactNativeModuleConfig.toMapString()}")

        reactNativeModules.add(reactNativeModuleConfig)
      } else {
        this.logger.info("${LOG_PREFIX}Skipping native module '${name}'")
      }
    }

    return reactNativeModules
  }
}

How are we supposed to create new RN project? When instructions on main RN site are followed, 2.0.1 of global CLI is installed and this error happens.

I also cant run mu RN app because of this (windows). It was running yesterday and just stopped.

Hey! Thanks for filing this issue. Unfortunately we don't have Windows machines to test on. We used to do node ./node_modules/.bin/react-native config, but we got a report that it doesn't work on Windows, but ./node_modules/.bin/react-native config works. So could you share some more information about that?

None of the developers of RN cli have windows machine?

None, could you believe that?

Sure I can believe it :-), but that's the big problem... At this moment, you start new RN project on windows, do everything described on official site, and it doesn't run. Is there a way to help?

If it worked for you yesterday, then likely your environment is ok with running:

node ./node_modules/.bin/react-native config

(inside node_modules/@react-native-community/cli-platform-android/native_modules.gradle).

It baffles me how hard it is to get regular node scripts running in Windows. Since you guys are experienced in working with this environment, we expect some help and your expertise.

We could technically run this cross-platform using npx. However I'm not sure if it doesn't output anything else in some circumstances. For example we used to call this command with yarn if it was used in a project, but it turned out that with some versions of Yarn and some versions of Node it produced output that was breaking either Android or iOS scripts. It's not easy unfortunately.

I wonder if running through node.cmd works universally on Windows?

node.cmd ./node_modules/.bin/react-native config

Could you folks try and let me know?

I wonder if running through node.cmd works universally on Windows?

node.cmd ./node_modules/.bin/react-native config

Could you folks try and let me know?

Got this error:

* What went wrong:
A problem occurred evaluating settings 'Mysue'.
> Text must not be null or empty

This node ./node_modules/.bin/react-native config, produces this:

`C:\wamp64\www\test-membrane\rn 01node_modules.bin\react-native:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')")
^^^^^^^

SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)`

@thymikee the script can execute in many different ways, the problem is that it never ends so the cli stays stuck on 0%. I tried the command with absolute path and it worked but got stuck. The only way to bypass it is to put a timeout.

The point is to make it work like you'd do this in you terminal (not running Android or anything):

yarn react-native config
# or
npx react-native config

How can I run it on Windows but without tools like yarn or npx? Or is there any guarantee I could get from npx (since the version bundled with Node 8.3) to not output _literally anything_ except from user script output?

There is npx on windows, and it works correctly. When I run npx react-native config, I get some json

yarn or npx both don't work via the java exec.

the problem is that it never ends so the cli stays stuck on 0%

Ok, so there's something we can do on performance side of things as well. This command traverses your file system a lot, which is known to be slow on Windows. We could probably make it faster, but that needs some investigation.

the problem is that it never ends so the cli stays stuck on 0%

Ok, so there's something we can do on performance side of things as well. This command traverses your file system a lot, which is known to be slow on Windows. We could probably make it faster, but that needs some investigation.

It's not slow, it never ends. Gradle never executes past cmdProcess.waitFor because it keeps waiting for the command to give the exit signal but it never does for some reason. Only putting a timeout makes it work.

There is npx on windows, and it works correctly. When I run npx react-native config, I get some json

Same as mine...

yarn or npx both don't work via the java exec.

Maybe java exec doesnt take PATH into consideration? Btw why is this important?

yarn or npx both don't work via the java exec.

Maybe java exec doesnt take PATH into consideration? Btw why is this important?

Because that's what's happening behind the scenes in the native_modules.gradle. Autolinking basically gets your packages through this command.

@theweavrs oh I know why it happens! Lemme construct the fix for that. In the meantime, can we all agree on what's the preferred way to run this command? We can also try/catch a bunch of them.

@thymikee great!

These are all the ways that seem to work (but need a timeout):

  1. powershell ./node_modules/.bin/react-native config
  2. Running the command with absolute path to /node_modules/.bin/react-native.cmd
  3. node ./node_modules/react-native/cli.js config

And these are the ones that don't need timeout but they return empty output:

  1. node ./node_modules/.bin/react-native config
  2. cmd ./node_modules/.bin/react-native config
  3. node.cmd ./node_modules/.bin/react-native config

@thymikee I applied your latest fix (by manually removing checkForNewRelease from cliEntry.js) and unfortunately, the issue remains. gradle exec does not exit and stays stuck on 0% INITIALIZING.

@thymikee from the JDK docs:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

So it seems we will need to read the output continuously using I/O stream classes. This article may be the solution.

@theweavrs while you're onto it, would you like to propose a good solution to that in a PR?

@thymikee working on it.

You're amazing, thank you!

@thymikee which command should I use?

For now we can go with node ./node_modules/react-native/cli.js config because it works in UNIX envs as well

@thymikee okay great!

Thank you all! I've just released v2.1.0 of the CLI, can you verify if that works? 🤞

Works for me. The time for command execution varies though. Sometimes, it completes in a few seconds and sometimes it takes exactly a minute. Currently, my internet is having issues so maybe it's because of that?

Congrats, previous error is solved. But now something else happens.

Previously I only had to run react-native run-android, and that command would first open JS server and then run the app on the emulator.

Now, it does not open JS server. I.e. it opens it and quickly closes it. So I need to run react-native start manually before running react-native run-android

Now, it does not open JS server. I.e. it opens it and quickly closes it.

@mi5ha I was having that issue even before this one. I thought it was only me...

I did not have that issue before the bug we are discussing.

Btw when I npm -g install react-native-cli it installs cli v.2.0.1

Btw when I npm -g install react-native-cli it installs cli v.2.0.1

There are 2 cli, react-native-cli is global. The local one is accessed via yarn react-native. Read the README.md of this project.

@mi5ha react-native-cli is something different. It's a global proxy to @react-native-community/cli used mostly for legacy init command.

You should go to you lock file, remove @react-native-community/cli entry and re-install, so it goes up to 2.1.0

it seems not working for me.
I use yarn add --dev @react-native-community/cli to install the new cli.
then react-native run-android
Same issue as before. but fix the compile issue.
Metro Bundler make a splash then automatically closed.
Still have to run react-native start to start it manually.

@zenz @mi5ha starting Metro is a separate issue, can you please create a new one for that? We changed some logic around that and that's likely the cause.

@zenz @mi5ha @thymikee created one at #484

@thymikee all right, then the new cli can pass the compile steps. but some error messages shown as previous commends said. like

:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "C:\Users\zenz\programs\test_app\android\.."): CreateProcess error=2, ϵͳҲָļ
:ReactNative:Automatic import of native modules failed.

> Configure project :app
:ReactNative:Cannot run program "./node_modules/.bin/react-native" (in directory "C:\Users\zenz\programs\test_app\android\.."): CreateProcess error=2, ϵͳҲָļ
:ReactNative:Automatic import of native modules failed.

Seeing ./node_modules/.bin/react-native proves you're not on the latest version. The command is now node node_modules/react-native/cli.js config. Remove the entry in lock file and install again. Or remove the whole lock file and install again if you have issues.

@zenz do what @thymikee said and if it still doesn't work, try adding these in your package.json under devDependencies:

    "@react-native-community/cli": "^2.1.0",
    "@react-native-community/cli-platform-android": "^2.1.0",
    "@react-native-community/cli-platform-ios": "^2.1.0",

Afterwards, do yarn or npm i

@theweavrs So instructions on official site are basically wrong?

I managed to make it work by:

  • Uninstalled global react-native-cli
  • Created new project with npx react-native init MyApp
  • Started JS server with npx react-native start
  • Run in android emulator with npx react-native run-android

And it worked! 🎉

@mi5ha not really but there are always several ways of doing things. Sometimes, one works and the other doesn't or one is easier to do than the other.

@theweavrs If you follow what is written on official site you get the solution that doesn't run, just saying... 😃

@theweavrs @thymikee yes! I removed yarn.lock and run yarn again. this time, the error message gone.

@mi5ha I don't think they updated it correctly/fully after 0.60 release and after npx was added.

@thymikee Today is deffinatelly not a good day 😭 Now this breaks npx react-native init xxx02 --template typescript

× Copying template error Error: Cannot find module 'C:\Users\misha\AppData\Local\Temp\rncli-init-template-c4zbhI\node_modules\react-native-template-typescript\template.config' error EBUSY: resource busy or locked, rmdir 'C:\wamp64\www\xxx02'. Run CLI with --verbose flag for more details. Error: EBUSY: resource busy or locked, rmdir 'C:\wamp64\www\xxx02' at Object.rmdirSync (fs.js:680:3) at rmdirSync (C:\Users\misha\AppData\Roaming\npm-cache\_npx\18548\node_modules\react-native\node_modules\fs-extra\lib\remove\rimraf.js:276:13) at Object.rimrafSync [as removeSync] (C:\Users\misha\AppData\Roaming\npm-cache\_npx\18548\node_modules\react-native\node_modules\fs-extra\lib\remove\rimraf.js:252:7) at Object.initialize [as func] (C:\Users\misha\AppData\Roaming\npm-cache\_npx\18548\node_modules\react-native\node_modules\@react-native-community\cli\build\commands\init\init.js:292:26) at process._tickCallback (internal/process/next_tick.js:68:7)

@mi5ha by using npx react-native init you're using a new init, which uses different templates. We're working on supporting typescript template here: https://github.com/react-native-community/cli/pull/483. Please bear with us.

For now, if you want to use template typescript, I suggest using the _legacy init_ through the react-native-cli package:

npx react-native-cli init xxx02 --template typescript

@thymikee Got it, so quite a turbulent times it seems. I confirm that previous command works.

Thanx

Yea, sorry for the confusion. We're in a big transition period, things are moving fast, but we're heading in a good direction, even if at times it seems like things get worse. It's only temporary :)

Hello guys, thanks for taking time and solving this problem!
I thought it'd be good to have a conclusion after all :)

This instructions worked for me:
1- Add dependencies below to devDependencies:

"@react-native-community/cli": "^2.1.0",
"@react-native-community/cli-platform-android": "^2.1.0",
"@react-native-community/cli-platform-ios": "^2.1.0"

2- Run yarn install
3- Run react-native run-android

And everything works fine! (till now at least lol)

** npm install -g react-native-cli still installs version @2.0.1 by the way - but version 2.0.1 doesn't work with RN 0.60.0

@mehranabi you should instead just delete the .lock files and run yarn/npm i. That is a far better solution than this one.

@theweavrs Hey, Thanks but i tried to update react-native-cli globally but it installed 2.0.1 so i had to do that in my comment

@mehranabi react-native-cli is just a proxy. See this comment

Let's get some facts straight:

  1. @react-native-community/cli is a dependency of react-native. That makes it a "transitive dependency" of your project.
  2. react-native-cli is a global package, we discourage to use it, it's kind of a proxy to @react-native-community/cli, but it's a bit more complicated that that.
  3. We update the CLI independently of React Native itself.

Now that we know the facts:

Solution is to update your CLI version:

  1. If you use lock files (_yarn.lock_, _package-lock.json_) - find the @react-native-community/cli entry, remove it, run yarn install/npm install once again

    1. If you don't use lock files – you should. But in such case, remove node_modules and run npm install again.

  2. run npm list @react-native-community/cli or yarn list @react-native-community/cli and verify you're on the latest version (above 2.1.0).

After performing these steps you should be on the latest CLI version. Feel free to do it once in a while, because we release often.

@theweavrs , Oh yes, I missed that comment! Thanks
Got it @thymikee , Thank you for clearification!

@thymikee We should add this Updating Guide to the README. Also the facts :D

Yes please! PR welcome <3

I already did all the previous steps and nothing solved
Even following the steps the problem persists.

Environment

System:
    OS: Windows 10
Yarn version: 
  1.16.0

Node version: 
  10.15.3

Platform: 
  win32 x64

react-native --version

react-native-cli: 2.0.1
react-native: 0.60.0

react-native run-android

`D:\ADS\curso-react-native\ReactNativeReduxGraphQl>react-native run-android
info Starting JS server...
info Installing the app...

Task :app:installDebug
01:20:29 V/ddms: execute: running am get-config
01:20:29 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
01:20:29 V/ddms: execute: returning
Installing APK 'app-debug.apk' on 'Pixel_3_API_28_86(AVD) - 9' for app:debug
01:20:29 D/app-debug.apk: Uploading app-debug.apk onto device 'emulator-5554'
01:20:29 D/Device: Uploading file onto device 'emulator-5554'
01:20:29 D/ddms: Reading file permision of D:\ADS\curso-react-native\ReactNativeReduxGraphQl\android\app\build\outputs\apk\debug\app-debug.apk as: rwx------
01:20:29 V/ddms: execute: running pm install -r -t "/data/local/tmp/app-debug.apk"
01:20:30 V/ddms: execute 'pm install -r -t "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read: -1
01:20:30 V/ddms: execute: returning
01:20:30 V/ddms: execute: running rm "/data/local/tmp/app-debug.apk"
01:20:30 V/ddms: execute 'rm "/data/local/tmp/app-debug.apk"' on 'emulator-5554' : EOF hit. Read: -1
01:20:30 V/ddms: execute: returning
Installed on 1 device.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.4.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 3s
27 actionable tasks: 2 executed, 25 up-to-date
'C:\Users\Anderson' n�o � reconhecido como um comando interno
ou externo, um programa oper�vel ou um arquivo em lotes.
info Connecting to the development server...
info Starting the app...
Starting: Intent { cmp=com.reactnativereduxgraphql/.MainActivity }

D:\ADS\curso-react-native\ReactNativeReduxGraphQl>`

Works only by executing commands
1st react-native start
2nd react-native run-android

or manually
the project repositori
https://github.com/Andersonfrfilho/ReactNativeReduxGraphQl

@Andersonfrfilho
you need another patch
https://github.com/react-native-community/cli/issues/484

Thx, moved.

@theweavrs So instructions on official site are basically wrong?

I managed to make it work by:

  • Uninstalled global react-native-cli
  • Created new project with npx react-native init MyApp
  • Started JS server with npx react-native start
  • Run in android emulator with npx react-native run-android

And it worked! 🎉

Thanks a lot. It worked

i found out trying new project react-native init awesomeproject Some problem with dependency issue., but start gradlew clean without any problem. (Windows Machine)

I've tried remove "node_modules" folder, "yarn.lock" (i use yarn),
Run yarn cache clean , and yarn install
It did works.

nothing is working!

anyone have experience getting this error on Azure DevOps? This is where we are continuing to get this error.

Was this page helpful?
0 / 5 - 0 ratings