react-native: NODE_BINARY in .xcode.env not working

Description

I would like to use the .xcode.env to set my node version. My .xcode.env looks like:

export NODE_BINARY=$(command -v node)

When I execute this command in a terminal window, it returns the proper node version. I’ve tested, and the only way I can set the node version is using Build Phases -> Bunde React Native Code and Images -> and exporting NODE_BINARY in the shell commands there to the absolute path:

export NODE_BINARY=/Users/myuser/.nvm/versions/node/v14.18.1/bin/node

Can anyone suggest why NODE_BINARY is not being set through the .xcode.env file? Do I have to take any extra steps to use this during archive?

Version

0.70.4

Output of npx react-native info

System: OS: macOS 12.6 CPU: (12) x64 Intel® Core™ i7-9750H CPU @ 2.60GHz Memory: 23.84 MB / 32.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node Yarn: 1.22.18 - ~/.nvm/versions/node/v14.18.1/bin/yarn npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm Watchman: 2022.03.21.00 - /usr/local/bin/watchman Managers: CocoaPods: 1.11.3 - /Users/micahsklut/.rbenv/shims/pod SDKs: iOS SDK: Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1 Android SDK: API Levels: 29, 30, 31 Build Tools: 29.0.2, 30.0.2, 30.0.3, 31.0.0 System Images: android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom_64, android-30 | Google APIs Intel x86 Atom Android NDK: Not Found IDEs: Android Studio: 2021.3 AI-213.7172.25.2113.9123335 Xcode: 14.2/14C18 - /usr/bin/xcodebuild Languages: Java: 1.8.0_292 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.1.0 => 18.1.0 react-native: ^0.70.4 => 0.70.6 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

Run Archive. The system node is being used, unless I set it as explained above.

Snack, code example, screenshot, or link to a repository

Screen Shot 2022-12-15 at 6 29 09 PM

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 37 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Loading nvm from within .xcode.env seems to resolve this for me:

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

export NODE_BINARY=$(command -v node)

nvm is common enough that I would suggest this should work out of the box.

The problem is that Xcode scripts works in a different environment than a regular terminal/ Xcode also overrides some basic ENV var that can shadow and mask other variables we might set.

If you don’t have node in the default PATHs, like /usr/local/bin, for example, Xcode is not able to find it. A simple solution could be to create a symlink to node in /usr/local/bin:

sudo ln -s $(command -v node) /usr/local/bin/node

We are investigating better way to handle this, but we haven’t found a robust approach yet, unfortunately. 😦

Apparently this is caused by nvm A less invasive workaround (that breaks nvm):

ln -s $(which node) /usr/local/bin/node

what worked for me:

cd ios
echo export NODE_BINARY=$(command -v node) > .xcode.env.local

This is also described as the proper way in react-native-xcode.sh as the (now deprecated) script for finding node versions, is not reliable

which command shows me : command: shell built-in command but I do some tests and I realize that nothing going work with Xcode build and .xcode.env. Now my .xcode.env is:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use

nvm use default # I need to put this because when use nvm current returns none

export NODE_BINARY=$NVM_DIR/versions/node/v20.7.0/bin/node

echo $NVM_DIR # Prints /Users/luciano.lima/.nvm
echo $NODE_BINARY # Prints /Users/luciano.lima/.nvm/versions/node/v20.7.0/bin/node

And, on Xcode log, I have these messages:

Now using node v20.7.0 (npm v10.1.0)
/Users/luciano.lima/.nvm # echo $NVM_DIR
/Users/luciano.lima/.nvm/versions/node/v20.7.0/bin/node # echo $NODE_BINARY
Node found at: /Users/luciano.lima/.nvm/versions/node/v20.7.0/bin/node

And some time later:

+ type node
+ echo 'error: node not found! Modules won'\''t be collected.' 'Please export NODE_BINARY in '\''Build Phase'\'' - '\''Bundle React Native code and images'\''' 'to an absolute path of your node binary. Check your node path by '\''which node'\''.'
error: node not found! Modules won't be collected. Please export NODE_BINARY in 'Build Phase' - 'Bundle React Native code and images' to an absolute path of your node binary. Check your node path by 'which node'.
+ exit 0
Command PhaseScriptExecution emitted errors but did not return a nonzero exit code to indicate failure

So, even using an absolute path to NODE_BINARY, build script phase fails.

The only way that make things works is with symlink like @cipolleschi suggests. Now my .xcode.env have only this:

export NODE_BINARY=/usr/local/bin/node

And now finally works. 🤷

in my case it was NVM. Either let nvm go or reconfigure it In your build phases scripts

if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
. "$HOME/.nvm/nvm.sh"
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
. "$(brew --prefix nvm)/nvm.sh"
fi

That solution looks like it works only if you have no xcode.env file. What about regenerating from existing projects or nodejs version upgrades?

@FrederickEngelhardt this can’t be done. If users customize their .xcode.env file with other configurations, we would wipe that away. Clearly, not a great experience.

I got the following setup on my local:

  • macOS Sonoma 14.5
  • nvm (without brew installation)
  • .nvmrc file on my project’s root folder
  • React Native 0.74.3
  • I have not run the sudo ln -s $(command -v node) /usr/local/bin/node command

I did almost the same modifications as @lucianomlima (here) to my .xcode.env. Here’s the complete file:

# This `.xcode.env` file is versioned and is used to source the environment
# used when running script phases inside Xcode.
# To customize your local environment, you can create an `.xcode.env.local`
# file that is not versioned.
# NODE_BINARY variable contains the PATH to the node executable.
#
# Customize the NODE_BINARY variable here.

# Usage with nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use $(<../.nvmrc) # Considering that .xcode.env is at ios/ and .nvmrc is at project's root
export NODE_BINARY="$(command -v node)"

and it’s working! 🎉

@vinnyA3 Many thanks! Yes that thread exactly mentions this! And I’ve gone for the simple solution of removing the file in my yarn commands.

I just spent half a day debugging a strange problem. XCode seems to be exiting with error code 65 due to the autogenerated .xcode.env.local. On deleting that file the next xcode-build command works fine. Here’s the sequence of events.

  1. Everything working fine for my mobile app codebase for deployment to the local iOS simulator
  2. Upgraded to RN 0.74.1 (don’t know if this is related though)
  3. Ran local deployment react-native run-ios to iOS simulator, everything works fine.
  4. In the next run of react-native run-ios, xcode-build exits with an error code 65 .
  5. I try cleaning the various generated dirs using these two yarn commands "cleanstall": "rm -fr node_modules ios/Podfile.lock ios/Pods ios/build ~/Library/Developer/Xcode/DerivedData && yarn install" and "run-ios": "cd ios && rm -f Podfile.lock && pod repo update && NO_FLIPPER=1 RCT_NEW_ARCH_ENABLED=1 pod install && cd .. && react-native run-ios --verbose", but problem persists.
  6. Opening project in XCode and debugging it there shows it exits just after accessing .xcode.env.local which has this line export NODE_BINARY=/var/folders/_r/51ztwzn17fd1qzm2l87zjzz40000gn/T/yarn--1716631514203-0.5113628975765909/node. I think strange because .xcode.env has export NODE_BINARY=$(command -v node) and that just works fine to point out the right node binary.
  7. I delete .xcode.env.local and do react-native run-ios again and things work fine again.
  8. I find this thread and try to reproduce the sequence of events so I could add screenshots etc here, but 3 does NOT fail anymore!

Apologies for the long description, just trying to paint as detailed a picture so something clicks for someone who knows the picture deeper. This behaviour is worrisome because its an obscure problem that might revisit in a couple of months and burn another half day.

Are there any preventive measures I can take? For example I was thinking of making .xcode.env.local the same as .xcode.env so it also has export NODE_BINARY=$(command -v node) and committing that to the repo (so far these files were being mentioned in .gitignore )

Apparently this is caused by nvm A less invasive workaround (that breaks nvm):

ln -s $(which node) /usr/local/bin/node

The problem is that Xcode scripts works in a different environment than a regular terminal/ Xcode also overrides some basic ENV var that can shadow and mask other variables we might set. If you don’t have node in the default PATHs, like /usr/local/bin, for example, Xcode is not able to find it. A simple solution could be to create a symlink to node in /usr/local/bin:

sudo ln -s $(command -v node) /usr/local/bin/node

We are investigating better way to handle this, but we haven’t found a robust approach yet, unfortunately. 😦

This analysis is correct. I solved this problem by completely uninstalling node and then downloading the .pkg installation from the node.js ,because the installation location of the installation package is /usr/local/bin/node by default.

Thanks for good answer. The important thing is uninstalling unused node version.

The problem is that Xcode scripts works in a different environment than a regular terminal/ Xcode also overrides some basic ENV var that can shadow and mask other variables we might set.

If you don’t have node in the default PATHs, like /usr/local/bin, for example, Xcode is not able to find it. A simple solution could be to create a symlink to node in /usr/local/bin:

sudo ln -s $(command -v node) /usr/local/bin/node

We are investigating better way to handle this, but we haven’t found a robust approach yet, unfortunately. 😦

This analysis is correct. I solved this problem by completely uninstalling node and then downloading the .pkg installation from the node.js ,because the installation location of the installation package is /usr/local/bin/node by default.

@cipolleschi thanks, they are on two lines in the file and it was auto generated like that! i commented out

export NODE_BINARY=$(command -v node)

and updated the other with my node path

export NODE_BINARY=/usr/local/bin/node

project now builds

Thanks

Hey @adamjw3! Your Xcode env has 2 export NODE_BINARY on the same line. That would not work.

You should have either

export NODE_BINARY=$(command -v node)

Or

export NODE_BINARY=/usr/local/bin/node

@lucianomlima yeah, that’s definitely very weird. Can you try add a script phase after building just calling which command? 😕

The problem is that Xcode scripts works in a different environment than a regular terminal/ Xcode also overrides some basic ENV var that can shadow and mask other variables we might set.

If you don’t have node in the default PATHs, like /usr/local/bin, for example, Xcode is not able to find it. A simple solution could be to create a symlink to node in /usr/local/bin:

sudo ln -s $(command -v node) /usr/local/bin/node

We are investigating better way to handle this, but we haven’t found a robust approach yet, unfortunately. 😦

In my case, Xcode can find node (using the ,export NVM_DIR=path/to/.nvm approach) but can’t find command, that is a built-in shell library. Sounds weird.