NVM breaks sporadically because of resetting PREFIX environment variable - react-native

The Problem
I am trying to run a react-native iOS application from the CLI. This has not been a problem in the past, but haven't developed in react-native for over 6 months. Last week in about 40% of the cases when a (zsh) terminal is opened nvm breaks because of the environment variables. This could happen whether I open the terminal myself, or when VSCode opens a new terminal for the metro bundler. Running from Xcode always works. Closing all zsh terminals and then opening a new one seems to resolve the issue. Additionally, it happens (more) often in the terminal that is opened by VSCode for the metro bundler than when I open the terminals myself. Lastly, the issue seems to 'stick' once it starts occurring I need to close all terminals for the error to be resolved.
Error that is shown
nvm is not compatible with the "PREFIX" environment variable: currently set to "/usr/local"
Run `unset PREFIX` to unset it.
nvm is not compatible with the "PREFIX" environment variable: currently set to "/usr/local"
Run `unset PREFIX` to unset it.
What I have tried
Changing environment variables
1.1 When the zsh terminal does NOT show the error:
I tried running npm config delete prefix, in hopes of changing the config so that the issue does not pop up anymore. I can't run this command when the error is shown, because I have no access to node or npm.
1.2 When the zsh terminal does show the error:
I can 'fix' the terminal by unsetting the PREFIX variable with unset PREFIX and then asking nvm to use a version of node. However this only fixes that particular instance of the terminals. The only sure fire way is to close all terminals.
unset PREFIX
nvm use v14.15.0
Updating react-native
Speaks for itself, a colleague mentioned that it might be caused by a bug in react-native. Did not resolve the issue unfortunately.
Conclusion
It seems as though something (re)triggers setting the PREFIX environment variable when I try to run the react-native iOS project. I am honestly completely lost as to what might be causing this and any help would be truly appreciated.

I had this issue too, as have others.
Are you using yarn or npm to run the React Native CLI? If so, and these were installed with Homebrew, these might be conflicting with your nvm installed node. Try removing these:
brew uninstall yarn
brew uninstall npm
This should leave behind the version of npm that was installed with nvm, check this by running:
which npm
This should return a path pointing to your local .nvm installation. If it shows /usr/local then it was not removed correctly, you might need to follow the npm removal instructions.
Once this is done, if you use yarn then install it with npm:
npm install -g yarn
Run the React Native CLI once again, it should now work.
More details on this resolution: https://github.com/nvm-sh/nvm/issues/1645#issuecomment-850376572

Related

React Native Fails To Start Server With Error In #react-native-community/cli/build/commands/doctor/healthchecks/index.js

I just created a brand new React Native project, and I can't get it to run. The server always fails on the following:
node_modules/react-native/node_modules/#react-native-community/cli/build/commands/doctor/healthchecks/index.js:48
} catch {}
^
SyntaxError: Unexpected token {
The only thing I've found on this issue is this thread, which ends with people saying to post this question on Stackoverflow, hence why I'm here. I've tried the usual: upgrading node versions, removing node_modules and reinstalling, restarting my machine, all to no avail. I even delved into the code to see if I could figure out what's going on, but nothing jumped out at me.
My hope is that others have run into this with new React Native projects and that someone can help me resolve it. Any suggestions are welcome. Thanks!
Sigh.... Well after struggling with this for a day I found the workaround was to run npx react-native start in one console and then run npx react-native run-ios in another. I was just trying to do the latter by itself (which always worked in the past), and I guess that doesn't work anymore.
Probably your node and/or npm version is to old. Check by npm -v and node -v and update to latest version, or suggesting by react-native crew in dependencies from node site. Or if you use nvm try:
nvm install 16.14.0 && nvm alias default 16.14.0 && nvm use 16.14.0
Note that, this is for specific version. For latest, type:
nvm install --lts --latest-npm
nvm use x.x.x //latest installed version
This problem is due to node version. Upgrade your node verion above 12. It will solve the problem.
Use can either use nvm or directly install the node
if you are using android studio start it with command line
like this cd /path/to/android-studio/bin
then studio.sh make sure its executable

nvm installing yarn either on a Node version folder or in one folder regardless of Node version?

It is a bit strange that on my MacBook Air, npm installed yarn on
$ which yarn
/Users/peter/.npm-global/bin/yarn
but on my other iMac, it is installed on a "per Node version folder":
$ which yarn
/Users/peterimac/.nvm/versions/node/v16.0.0/bin/yarn
Which one is more correct, and should I and how do I adjust the other Mac back to the "correct" mode? But on the iMac, it is strange that if I do
yarn use 16
yarn build
and then it says to use yarn global add serve and then serve -s build but it kept on saying serve does exist no matter how I source ~/.profile and source ~/.zprofile. I have to npm i -g serve for it to exist.
Also, on the first Mac, the yarn and serve also do not need to be installed for any different versions of Node. The same version of yarn and serve seemed to be used no matter which version of Node I change to using nvm use 14 or nvm use 16. So I am not sure if this is the correct behavior.
Is one Mac more correct than the other, and how to fix it back to the correct way and what about the serve not installed issue?
I found part of the answer: if your yarn is at
/Users/peter/.npm-global/bin/yarn
that means some time in the past, you have set .npm-global to be a one and only path... and that would conflict with nvm. So remove that line from your ~/.npmrc, and also remove the .npm-global text or line from your ~/.profile, and .zprofile, .bashrc, .zshrc, if any.

Why is preact not getting recognized in the terminal even after installing it?

I have installed Preact but it’s not getting recognized in the terminal. First, I went to the official website of PreactJS. The doc says that I have to run npm install -g preact-cli. As I am using Yarn, I ran yarn global add preact-cli. It was installed successfully but with a lot of warnings. Then I ran preact create default first-preact-app. But it's showing an error that "The term 'Preact' is not recognized as a name of a cmdlet, function, script file or executable program."
I have tried force cleaning the cache and reinstalling preact-cli. But it doesn't work. Two of the warnings are that preact and preact-render-to-string have unmet peer dependency. So, I have installed them also. But it still doesn't work.
I have tried running npm and experimented with the commands also. But it didn't work. So, how can I fix the problem?
I have run these commands using Powershell and Git bash on Windows 10. I am trying to install Preact 10.5.12 using Yarn 1.22.5. And a point to be noted, I tried running yarn dlx but it returns an error that "command not found". And when the installation of preact and preact-render-to-string gets finished, yarn gives and warning that they have no binaries.
EDIT: Perhaps, it was a problem with Windows. The problem has gone after reinstalling it.
Your problem almost certainly is that preact-cli was not on your PATH - a list of programs that are globally accessible.
For what it's worth, we don't recommend installing globally. The site may still say that but the repository correctly recommends using npx instead.

'vue create xxx' - nothing happens

I have installed the latest Vue CLI, with npm install -g #vue/cli#next, having uninstalled the previous 'vue-cli'. Now when I try and create a Vue application with e.g. vue create vue3-todo, nothing happens. The command quickly executes without error (but without any prompts), and nothing is created - the directory remains empty.
Clearly something is not right with my Vue CLI installation. Any suggestions as to what the issue might be and how I can clean things up?
I would start with running this to uninstall:
npm uninstall -g vue-cli
Then check my node version. It's recommended to use node v10+ for the latest CLI.
I'd probably do a system restart just because and then reinstall:
npm install -g #vue/cli
I have got to the bottom of this. This is under Windows 10, and I noticed there was a an entry of 'C:\Program Files\nodejs' in the PATH environment variable (under 'System variables'). I looked in that directory, and sure enough, there were a bunch of 'vue*' files. I deleted these and now when I call 'vue' I get the current version as I should. Clearly these were taking precedence over the versions in my AppData/Roamin/npm directory.
I have no idea why these files were there in the first place. I am hoping that having deleted them I do not break anything.

expo is not recognized as an internal or external command

I am having trouble getting expo start to work.
I completely uninstalled both expo and expo-cli with npm and yarn and then installed expo-cli globally with npm and I still get the same error. I think it has something to do with the most recent expo-cli update, because it was working perfectly fine. So how can I install an older version of expo-cli?
I also don't see any path in the environment variables? Could that have something to do it?
For some reason, the update messed with the path.
So I had to go to edit environment variables and add another line to the variable "Path" under the system variables.
Don't hit new. Click on "Path" under the system variables section and add a new line there. and use "%USERPROFILE%\AppData\Roaming\npm".
Then open a new command line and it should work.