Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm use in build.gradle when using Android Studio #687

Closed
davidstoker opened this issue Nov 23, 2016 · 26 comments
Closed

npm use in build.gradle when using Android Studio #687

davidstoker opened this issue Nov 23, 2016 · 26 comments
Assignees

Comments

@davidstoker
Copy link

davidstoker commented Nov 23, 2016

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?

@stlemon
Copy link

stlemon commented Nov 28, 2016

I met the same problem.

@stewart42
Copy link

I have this issue as well.

@dccarmo
Copy link

dccarmo commented Nov 28, 2016

Having the same issue, I wasn't even aware it was related to Realm.

@cobarx
Copy link

cobarx commented Nov 28, 2016

This would be great to get fixed pretty soon. It makes it hard to work in Android Studio.

@Nicomalacho
Copy link

i have the same issue.

@tristanbbq
Copy link

Same here, changing to /usr/local/bin/npm does fix the issue temporarily.

@nhachicha
Copy link
Contributor

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

@zhenglingxiao
Copy link

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.

@tristanbbq
Copy link

@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/ ?

@zhenglingxiao
Copy link

In macOS Sierra,this 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.

@jatazoulja
Copy link

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.

@nhachicha
Copy link
Contributor

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,

@cobarx
Copy link

cobarx commented Dec 29, 2016

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.

@berkayk
Copy link

berkayk commented Jan 11, 2017

Having the same problem on windows as well.

@iSimonWeb
Copy link

Same here, darn.

@anders-g-hansen
Copy link

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()
}

@DomiR
Copy link

DomiR commented Feb 3, 2017

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.

@nhachicha
Copy link
Contributor

this should be fixed by #849

@L3V147H4N
Copy link

👍

@kristiandupont
Copy link
Contributor

Is anybody here still having trouble with this or can I close it?

@evollu
Copy link

evollu commented Mar 8, 2017

I'm still having the trouble (mac user)

@blagoev
Copy link
Contributor

blagoev commented Mar 15, 2017

@evollu Can you check you are using the latest version of realm? I can't reproduce the issue with the latest changes on mac.

@evollu
Copy link

evollu commented Mar 16, 2017

@blagoev with android studio?

@blagoev
Copy link
Contributor

blagoev commented Mar 16, 2017

@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
acf315f

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.

@evollu
Copy link

evollu commented Mar 17, 2017

yeah i tested in 1.1.1 and everything seems fine

@blagoev
Copy link
Contributor

blagoev commented Mar 17, 2017

thanks, closing this one

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests