Seeliang
Seeliang

Reputation: 2949

React Native ios build : Can't find node

I have a prototype ready to go and the project is jammed with build:

error: Can't find 'node' binary to build React Native bundle If you have non-standard nodejs installation, select your project in Xcode, find 'Build Phases' - 'Bundle React Native code and images' and change NODE_BINARY to absolute path to your node executable (you can find it by invoking 'which node' in the terminal)

this feedback is helpless for me, i do have node with nvm. is this something related to bash?

Upvotes: 127

Views: 118241

Answers (22)

Towhidul Islam
Towhidul Islam

Reputation: 51

This error occurs if the path of the "node" changes somehow. I have solved this issue by using this command:

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

Upvotes: 2

Jadent
Jadent

Reputation: 1404

If you run into this issue when building your iOS project using react-native 0.73 then this issue may be caused by the presence of an ios/.xcode.env.local file which is setting the NODE_BINARY environment variable during the iOS build to a temporary node location. This can happen if you are using using Yarn.

See also this related RN project issue https://github.com/facebook/react-native/issues/43285

Upvotes: 1

Alex
Alex

Reputation: 842

For anyone that stumbles upon this in 2022 here is my situation and how I fixed it.

  1. react-native --version -> 4.14
  2. npx --version -> 6.14.9
  3. node --version -> 12.14.1
  4. We use TypeScript also.

Xcode "Bundle React Native code and images"

Xcode settings, build phases, bundle react native code and images location

export ENTRY_FILE=src/App.tsx
../node_modules/react-native/scripts/react-native-xcode.sh
../node_modules/expo-constants/scripts/get-app-config-ios.sh
../node_modules/expo-updates/scripts/create-manifest-ios.sh

Remove the export NODE_BINARY=node line of code. It's not needed anymore. The way I figured this out was reading through the source code found in ../node_modules/react-native/scripts/react-native-xcode.sh If you look there, you'll find nvm/node sourcing being done for you.

Upvotes: 18

Roberto
Roberto

Reputation: 381

If you are developing a React Native based solution and you are using NVM for local Node versioning, the error may be due to this.

Xcode cannot find the Node version, of course Xcode fetches the Node in the / usr / local / bin / node directory and NVM stores the Node in another directory like Users / $ {my-user} /. Nvm / versions /node/v14.16.0/bin/node

To work it is enough to create an alias for Xcode to find the Node in its default search:

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

Upvotes: 28

Bruno Thuma
Bruno Thuma

Reputation: 11

In my case I am not fixing the React Native project but I use a React Native integration in my native project. Some dependencies were not seeing the node I had installed using homebrew, although I had it on PATH.

For anyone that stumbles upon the same situation just make sure the /usr/local/bin directory exists and run

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

as mentioned by @F Mamali and @josesuero

Upvotes: 1

Al Momo
Al Momo

Reputation: 65

If you are using NVM, then the usual cause of this error is NVM not properly initialized in the Bundle React Native Code And Images Build Phase.

To initialize NVM just change the Build Phase script like so:

source $HOME/.zshrc
# If you initialize NVM using .profile uncomment this line:
# source $HOME/.profile
export NODE_BINARY=$(which node)
../node_modules/react-native/scripts/react-native-xcode.sh

Upvotes: 1

I will give you the quickest solution not the best.

Open terminal and write which node or command -v node to find out where is your node. It will give you path for me its: /opt/homebrew/bin/node.

Then go to script_phases.sh file under your PROJECT_ROOT/node_modules/react-native/scripts/react-native-pods-utils/script_phases.sh and paste this top of the file.

NODE_BINARY="/opt/homebrew/bin/node"

You might do the same thing for react-native-xcode.sh under the PROJECT_ROOT/node_modules/react-native/scripts/.

Upvotes: 0

Preetika
Preetika

Reputation: 804

If this command says /usr/local/bin/node: File exists you need to know that the link already exists to maybe a different version of node. In my case, to install yarn, brew installed a separate nodejs v15 and linked the file to its binary. Although I use nvm to have nodejs v14 and nodejs v16. This extra nodejs was the reason for the error mentioned in question.

Simply run sudo rm -f /usr/local/bin/node to remove the link followed by the command sudo ln -s $(which node) /usr/local/bin/node to create correct link.

Upvotes: 4

EddiG
EddiG

Reputation: 202

If you use the NVM tool to manage different versions of the Node then creating a symbolic link to the current default Node version ln -s $(which node) /usr/local/bin/node could break the other projects those expect another version. That happens as in the PATH environment variable the path /usr/local/bin goes before the path of the Node selected by NVM.

React Native and Expo uses the bash shell for build scripts and therefore we just need to set up the bash shell properly to work with NVM. To do that copy the configuration code from the ~/.zshenv into ~/.bash_profile without taking the autocompletion part:

# This file is for the React Native development as it requires the bash shell in the build scripts

# Configure NVM
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh"

Upvotes: 1

Deivy Hernández
Deivy Hernández

Reputation: 99

For users who migrate to react-native 0.70.* and get a similar error, I fixed it by adding in Build Phases section:

set -e

WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"

/bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"

enter image description here

And in the iOS directory create the file .xcode.env.local (doc)

# 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.
# For example, to use nvm with brew, add the following line
# . "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(which node)

Upvotes: 6

Glenn
Glenn

Reputation: 2206

In 2022 the default build looks in ios/.xcode.env.local and ios/.xcode.env for environment customizations. The provided .xcode.env has an example enabling nvm and setting NODE_BINARY.

This is configured in Build Phases -> Bundle React Native code and images. The shell script looks as follows circa 10/2022:

set -e

WITH_ENVIRONMENT="../node_modules/react-native/scripts/xcode/with-environment.sh"
REACT_NATIVE_XCODE="../node_modules/react-native/scripts/react-native-xcode.sh"

/bin/sh -c "$WITH_ENVIRONMENT $REACT_NATIVE_XCODE"

The Input Files section for this script has

$(SRCROOT)/.xcode.env.local
$(SRCROOT)/.xcode.env

The default .xcode.env looks like this now:

# 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.
# For example, to use nvm with brew, add the following line
#. "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)

To reliably get node set, configure your project properties to the current default values and update one of the two reference env files.

My ios/.xcode.env looks like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
export NODE_BINARY=$(command -v node)

Upvotes: 17

Ajmal Hasan
Ajmal Hasan

Reputation: 1569

Simple latest working solution:

Just replace this line in shell:

export NODE_BINARY=node ../node_modules/react-native/scripts/react-native-xcode.sh

ScreenShot

Upvotes: 1

Abdelalim Hassouna
Abdelalim Hassouna

Reputation: 768

In my case, I had to choose a lower version of node
from node/18.7.0 to node/16.9.0


react-native: 0.66.3
Xcode: 14.1
MacOs: 12.6.1

Upvotes: 3

John F
John F

Reputation: 921

From the React Native environment setup documentation:

Starting from React Native version 0.69, it is possible to configure the Xcode environment using the .xcode.env file provided by the template.

The .xcode.env file contains an environment variable to export the path to the node executable in the NODE_BINARY variable. This is the suggested approach to decouple the build infrastructure from the system version of node. You should customize this variable with your own path or your own node version manager, if it differs from the default.

so in your project's root directory, look in the ios/ subdirectory, create or update the file .xcode.env to export your node binary:

file: /ios/.xcode.env

# To use nvm with brew, uncomment the line below
# . "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)

This file will be loaded by Xcode and properly provide the node binary for your build process. Note that if you're using NVM for your node environment, you'll need to uncomment the line to initialize your NVM environment before exporting the node binary.

Hope this helps! :)

Upvotes: 5

aprilmintacpineda
aprilmintacpineda

Reputation: 1274

The best solution is to actually do ln -s $(which node) /usr/local/bin, this will create a "symlink" from /usr/local/bin/node to wherever your node is, this is important because most apps looks at node at this path. You probably don't want to do export NODE_BINARY=[your node path] because when another teammate has a different OS or different installation of node (i.e., one is using nvm the other is using homebrew), then the same error will occur, it will also happen when building for production. So just go with ln -s $(which node) /usr/local/bin to be safe.

Upvotes: 6

Maxime B
Maxime B

Reputation: 641

Solution for nvm users :

In your build phases scripts, just add

# Fix for machines using nvm
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

Above export NODE_BINARY=node. This will make Xcode work regardless of your machine using nvm.

Upvotes: 49

user3009752
user3009752

Reputation: 184

If you are using Mac, check if you have installed node with brew. In this case it cannot find the path. Install node by downloading from the official website

Upvotes: -1

F Mamali
F Mamali

Reputation: 141

nvm alias default 14

OR

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

This made my Xcode 12.4 see node

Upvotes: 14

josesuero
josesuero

Reputation: 3760

@brunocascio solution on the comment is simpler and less invasive, create a symlink to node, on command line:

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

Update:

On new M1 Mac I had to cd /usr/local then mkdir bin (or just sudo mkdir /usr/local/bin) first.

thanks leo for the comment

Upvotes: 334

Seeliang
Seeliang

Reputation: 2949

I found one solution

First find your current node, in shell

which node

then copy your node url to

export NODE_BINARY=[your node path]
../node_modules/react-native/packager/react-native-xcode.sh to node_modules/react-native/scripts/react-native-xcode.sh

enter image description here

Upvotes: 104

TechnoTim
TechnoTim

Reputation: 3205

The solution for me is to set a default version of node with nvm in your profile. This works for bash or zsh:

Add this to your .zshrc or .bashrc

# default node version for nvm
nvm use 8.9.3

Be sure to change it to the version you want when starting a new terminal.

Upvotes: 3

Arunkumar
Arunkumar

Reputation: 1741

Open Xcode, then choose "Preferences..." from the Xcode menu.

Go to the Locations panel and install the tools by selecting the most recent version in the Command Line Tools dropdown.

select command line tools like as this image enter image description here

Upvotes: -7

Related Questions