Setting custom node version (path) for Neovim - node.js

TL;DR: I'd like to use node v14 for neovim only, and v8 for everything else. Suggestions?
Hello!
I have recently started using neovim for development purposes. I have installed several plugins, some of which require higher node versions than 8 - which is mandatory for my work environment. I used nvm, downloaded latest v14. Since nvm takes a ton of time to load up, I have added path in zshrc, which I simply changed from export PATH=../v8../bin to export PATH=../v14../bin. So far, so good. But my work repos throw errors, since they require node v8.
Now, I start up nvim on v14 in a session, then manually change path in zhsrc for work sessions to v8. This seems cumbersome to do every time. Is there a better way?

Use nvim environment variable: let g:node_host_prog = '/usr/local/bin/neovim-node-host'
See Neovim Provider Documentation

The following line can be added to your nvim/init.vim to prepend a directory DIR to your PATH when nvim is launched.
let $PATH = 'DIR:' . $PATH
Here's an example with the directory you specified.
let $PATH = '../v14../bin:' . $PATH

Related

How to change my version of node using a script?

I'm trying to write scripts that allow me to switch my development environment.
I want to write a script so that I can call it like this:
switchdevA
and inside that script, one of the things it should do is switch to node v16.16.0
inside switchdevA i have the following:
#!/bin/zsh
source ~/.zshrc
nvm use v16.16.0
the output says that I have switched to node v16.16.0, but when I type node -v I get:
v12.22.12
I can fix if i call source switchdevA, but should I do it that way? Other things like java don't seem to require me sourcing. In addition, the installation is lost if I start a new zsh session, ideally it should install for the current session and all future sessions.
What's the best practice around this?
A quick clarification: NVM is a shell function and not an executable.
You've correctly identified that source switchdevA will work properly, and that it is different from other version managers.
One solution is to keep your script as is, and create an additional shell alias that will run source scriptname (e.g. alias switchdevA='source switchdevA').
Unfortunately, for your other request to keep the current Node version in all future shell sessions, that can’t be achieved unless you have some persistence added in. For example, a simple file named ~/.lastnode. Then, in your ~/.zshrc, you would have a line nvm use "$(cat ~/.lastnode)" (which comes after the initialization of NVM). Finally, the original script would need to include a line at the bottom to update the last selected Node version: nvm version > ~/.lastnode.

how to upgrade node to 16 on ARM mac with macos 12.4

I have node v15.10.0 installed. I dont know how it was installed.
I found this article on how to update node. For mac it recommends using the official binary installer, which I did. It says the new version should overwrite the old version.
After installation, if I open a new terminal and do node --version, it stills says 15.10.0
The question is, how do I fully delete the old version, and install the new?
My path looks like this:
/Users/xx/.nvm/versions/node/v15.10.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Apple/usr/bin:/opt/apache-maven/bin
I presume I could hack the path using a shell script, but is there a better way? Why did the installer not do this for me?
If we have to hack the path (for each user), which is the correct file? I see there is a .bashrc and a .zprofile in my home dir. They are not linked, but they have identical content, which is bazare, and they do not contain code to add /Users/xx/.nvm/versions/node/v15.10.0/bin to the path, so this must be set outside of either file?
I cant see 15 path in /etc/paths either.
I have no idea what shell you get in Mac now a days, but if I do ps -ef I see it mentions both bash and zsh.
Presumably I need to change the path on both ?
if you have nvm installed, the best way is :
nvm install 16
To switch between installed node version:
nvm use 16
To set default node version:
nvm alias default 16
Thanks to #AKX this was the solution:
nvm uninstall 15.10.0
Now I see v16.

What is the best way to automatically switch between node versions?

When you work with two projects and one project depends on node v 8 and another one project depends on node v 10 you have to somehow switch between them.
Not convenient way:
This is not convenient because i have to always remember to type nvm use when i've just entered into root of project dir or when i've just opened new terminal session.
So when you are working on some project you can go in to root dir and run nvm use and it'll pick up node version from your .nvmrc and you can work on this project in this terminal session.
The same thing with another project - nvm use and work in this terminal session.
Convenient but not reliable way:
This is not way reliable because not all of devs have nvm exactly here $HOME/.nvm/nvm.sh and i don't like this hardcoding of nvm path because it's looks like some dirty hack.
Follow answers in this thread and get some working way. This means that just for my personal local developing it will work(i've tried this thread and it's not working because of this error and i don't know what it means)
$ source $HOME/.nvm/nvm.sh && nvm use && nodemon ts-node -r
tsconfig-paths/register src/main.ts nvm is not compatible with the
"PREFIX" environment variable: currently set to "/usr/local" Run
unset PREFIX to unset it.
Engines in package.json way
I've tried write "engines": {"node" : "10.14.1"}, in to my package.json but when i've started yarn - node haven't changed to 10.14.1(it's already installed on my laptop)
I don't know any more ways to conveniently and reliably change my node version just running yarn start. Do you know some way? Or may be you know some best practices?
After some time i've reinstalled my OS and i've tried to use ZSH terminal. There is zsh-nvm plugin which check .nvmrc every time you do cd. Convenient. So good so far.

How to install Node Version Manager(NVM) without admin rights

I have no admin rights in my windows machine. Can I install NVM without admin rights? I tried using the environment variable path setup, but its not working in my case.
I have the same need and couldn't find one, so I created one base on another simple nvm:
https://www.npmjs.com/package/#jchip/nvm
Requires powershell 4+ and permission to execute scripts.
(You're talking about https://github.com/coreybutler/nvm-windows right?)
Whether you can install it without admin rights aside, the actual act of switching node versions with it requires them so you're going to have trouble.
Your best bet is to install different versions of node into different paths manually, and then configure your environment variables to point to the right one whenever you need to use it.
eg. prefix your cmd script with PATH=C:\node\v10;%PATH% to have any node or npm calls in that script use whatever node is sitting in v10
try this
create a bat file like below
#cd C:\Users\testuser\AppData\Roaming\nvm
#SET PATH=C:\Users\testuser\AppData\Roaming\nvm\v14.21.1;%PATH%
cd c:\users\testuser\Desktop\Project
#cmd.exe /K
Run bat file and type
code .
It's open with VSCode
go to the terminal and type node and you can see the node version that you set in the bat file.
enter image description here
You can apply any node version as above bat file
If you use Git Bash on Windows, you can add this to your bash.bashrc to switch node versions:
export PATH=/c/path/to/node/dir:$PATH
Then just restart your terminal to pick up the updated PATH.
It will prepend your path with your desired node version. It's the only way I've found to override the installed node version if you don't have admin rights on your machine.

Node.JS - How to Run Node Command from any path?

I have installed the latest node.js from here . I see the %Path% variables have been set for Node.js. But, When i run node from C:\ , i get a "Node is not an recognized comman". But node command is working fine from "C:\Program Files (x86)\nodejs" path. Can i not run Node commands from C:\ or Say from an other path if i set the Env variables right? Please help. I am running a Win7 X64.
Thank You,
Faraaz
I had the exact same problem, but in my PATH variable I had: C:\Program Files\nodejs\.
After changing it to C:\Program Files\nodejs I could access it from anywhere.
What happens if you run node --version? I have never tried node.js from windows but if that doesn't work there is something wrong with your paths.
What do you get when you run echo %PATH%?
You should check if the path C:\Program Files (x86)\nodejs is present in your %PATH. If not, add it via the system properties (persistent change). In any case, restart your command prompt, it'll reload the %PATH variable. If you're lucky, it should work now :)
Most likely it happens because there is no Node path in your PATH environment variable.
I see two ways to solve this
Just add Node path into the end of PATH.
Reinstall Node as administrator
run 7-zip (or some file manager) with administrator privileges >>
browse to node-installer.msi >> open it, install node >> enjoy
My Sublime was working with Nodejs fine. I could press Alt-R and see the output from Node in Sublime. Then at some point I upgraded Sublime. I now have version 2.02 build 2221
And at some later point I tried Alt-R and got this infamous message
'node' is not recognized as an internal or external command
But it had worked, so what changed?
The steps I took to fix it were to install git bash and then
$ git clone https://github.com/tanepiper/SublimeText-Nodejs.git
$ cd SublimeText-Nodejs/
$ git checkout 095ba03344
Back in Sublime I clicked Preferences > Browse Packages. This opened up Windows Explorer with the folder:
C:\Users\me\AppData\Roaming\Sublime Text 2\Packages
I replaced the contents of the Nodejs folder with the git version. Re-started Sublime and then Alt-R sprang back into life.
Other things that turned out to be irrelevant were changing the file Nodejs.sublime-settings and tampering with system environment variables. The only thing that worked was 095ba03344 as described here
https://github.com/tanepiper/SublimeText-Nodejs/pull/39

Resources