An error is being thrown by the use of npm in this line of the build.gradle:
version = "npm --silent run get-version".execute(null, projectDir).text.trim()
I am using this in a React Native project. I typically build from the command line but attempted to build in Android Studio and the build fails with this error since it can't find the npm executable:
Cannot run program "npm" (in directory "/Users/<directory>/node_modules/realm/android"): error=2, No such file or directory
The default path for a GUI application like Android Studio on macOS is:
/usr/bin:/bin:/usr/sbin:/sbin
In my case, npm is in /usr/local/bin installed through homebrew.
If I change the command to a full path then it builds correctly:
version = "/usr/local/bin/npm --silent run get-version".execute(null, projectDir).text.trim()
Any suggestions for handling this in the publish_android_template instead of me manually correcting this?
I met the same problem.
I have this issue as well.
Having the same issue, I wasn't even aware it was related to Realm.
This would be great to get fixed pretty soon. It makes it hard to work in Android Studio.
i have the same issue.
Same here, changing to /usr/local/bin/npm does fix the issue temporarily.
Hi @davidstoker
Android Studio doesn't inherit the environment variables you defined in your shell.
we have similar problem in realm-java where we use launchctl setenv to work around the issue.
In your case a quick work around, is to start Android Studio from the shell (ex: on OSX) ~/Applications/Android\ Studio.app/Contents/MacOS/studio& this will inherit the PATH containing npm
Cheers
In my macOS Sierra, the following works for me.
1) Reboot mac and press cmd+r when booting up. Then input csrutil disable in utilities > terminal, and then reboot mac.
2) After reboot, now I can run sudo ln -s /usr/local/bin/npm /usr/bin/npm.
3) Make csrutil enable just like step 1.
@zhenglingxiao what is that csrutil disable and csrutil enable doing exactly? do you really need those steps to symlink usr/bin/npm -> /usr/local/bin/npm/ ?
In macOS Sierra锛宼his is System Integrity Protection. If we don't run csrutil enable, it will have the error Operation not permitted when run sudo ln -s /usr/local/bin/npm /usr/bin/npm. I found this in stackoverflow.
I am also having this issue, I am using a non standard RN directory structure (in which my android project is located somewhere else).
I have to comment out that line
// version = "npm --silent run get-version".execute(null, projectDir).text.trim()
in order to make it work... its a hack for me on my local machine but I think I need a permanent solution for this. having a multiple developers working on a project and having a CI would break builds.
Hi @jatazoulja
Have you tried one of the suggestions above?
This is an issue on AndroidStudio not using the environment variables
I don't see how this could be an issue for CI, I believe you're not using AndroidStudio within your CI machine to build your project, and you're rather building from command line which should find npm if the PATH is configured correctly
Cheers,
is it possible to manually set the path for that command like you would on the shell? Something like:
PATH=$PATH:/usr/local/bin npm --silent run get-version".execute(null, projectDir).text.trim()
Gradle won't accept this particular syntax, but there may be some variation that does work properly.
While the workarounds do handle this, nearly every Realm developer who's targeting iOS with React Native is going to run into this problem. Unless this is really well documented, it's going to frustrate a lot of people.
Having the same problem on windows as well.
Same here, darn.
Was seeing this problem on windows even though npm was in the path. Was able to resolve the issue by adding the npm file extension (see below).
task send(type: SendAnalyticsTask) {
applicationId = getAppId()
version = "npm.cmd --silent run get-version".execute(null, projectDir).text.trim()
}
What about nvm users?
This is /Users/*/.nvm/versions/node/v7.1.0/bin/node
Why not read version directly from package like the applicationId? The npm command only echoes the version anyways.
this should be fixed by https://github.com/realm/realm-js/pull/849
馃憤
Is anybody here still having trouble with this or can I close it?
I'm still having the trouble (mac user)
@evollu Can you check you are using the latest version of realm? I can't reproduce the issue with the latest changes on mac.
@blagoev with android studio?
@evollu Yep. Android Studio builds and runs the project fine.
Can you check this file in your project
node_modules/realm/android/build.gradle
then try finding the getNpmVersion function in that file
it should contain this fix
https://github.com/realm/realm-js/pull/849/commits/acf315f2d8b1a99a31758135244501967999b655
If it does and you are still not able to build from within Android Studio then there is something else going on in your project.
Also you can try creating a new default react native app just to test the fix is working within Android Studio for the new app.
yeah i tested in 1.1.1 and everything seems fine
thanks, closing this one
Most helpful comment
Hi @davidstoker
Android Studio doesn't inherit the environment variables you defined in your shell.
we have similar problem in realm-java where we use
launchctl setenvto work around the issue.In your case a quick work around, is to start Android Studio from the shell (ex: on OSX)
~/Applications/Android\ Studio.app/Contents/MacOS/studio&this will inherit thePATHcontainingnpmCheers