Using NVM Node from XCode

Posted on December 9, 2022
Tags: ios, xcode

If you try to use an nvm install of node from XCode, for instance in React Native using Sentry, you will likely get the following build error:

env: node: No such file or directory

This is because XCode uses a sanitised version of the PATH, and your .profile will not be used, so anything in there to adjust your PATH will not work.

/usr/local/bin is on your path, so my solution to this is to create a node script that uses the default version, or the one in .nvmrc if available.

#!/usr/bin/env sh
# Use the version of node specified in .nvmrc to run the supplied command

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

nvm_rc_version > /dev/null 2> /dev/null
HAS_NVM_RC=$?

if [[ "$HAS_NVM_RC" == "0" ]] ; then
        nvm run $*
else 
        nvm run default $*
fi
exit $?

Make sure to sudo chmod +x /usr/local/bin/node to make it executable.