How to set nvm and ESLint with VSCode globally? - eslint

I want vscode eslint extensions works with nvm.
I got "Install ESLint global with npm install -g eslint" in vscode eslint estension console.
So, I've installed the eslint with nvm in terminal with npm install -g eslint, and set it, which in the path:
/Users/myname/.nvm/versions/node/v10.16.0/lib/node_modules
Try to review the official docs, and find one solution to set eslint.nodePath in settings.json.
settings.json of vscode:
"eslint.nodePath": "/Users/myname/.nvm/versions/node/v10.16.0/lib/node_modules",
After set this property, the extension works well.
But, it would broke when I change node versions use nvm. I need to re-set settings.json.
Is there any solutions to set eslint in vscode with nvm?
I don't want to set settings.json when I change node versions with nvm each time.
I want to do it simple.
Thx.

I figure it out after few hours hard work.
do NOT use fish as mac's default shell
set vscode user settings to:
"terminal.integrated.shell.osx": "/usr/local/bin/fish",
that you can use fish in vscode terminal
install fisher,
use
fisher add FabioAntunes/fish-nvm
fisher add edc/bass
that you can use nvm with fish
set your .bash_profile of nvm and nvm variables:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
add
fish
at the bottom
If you want to switch bash and fisher use:
# fish -> bash
bash --login
# bash -> fish
fish
Enjoy fish and nvm and eslint vscode plugin
This will output of vscode console.
[Info - 12:04:44 PM] ESLint server stopped.
[Info - 12:04:45 PM] ESLint server running in node v10.11.0
[Info - 12:04:45 PM] ESLint server is running.

Related

Problem choosing node version in Terminal - MacOS Ventura

Whenever I start a new session in iTerm or the Terminal in MacOS Ventura, I get the system label on the right.
A problem I'm facing is that whenever I want to change the node version, it cannot be done. It's always tied to the system version.
This is when I run nvm list. For default it's the right value: 16.18.1. But when I run nvm use default, and run nvm list again, I still see system as the selected version.
Does anyone know why is still using the system one, instead of the default?
I have no idea about the terminal prompt that appears on the right side.
I had the same issue, and I just added the below lines to the .zshrc file:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" # This loads nvm bash_completion
To use the latest version or the version that I installed on my machine, I tried the following:
nvm use <node_version>
I hope this helps!

How can I call "nvm use" in a bash script, and have the selected node version retained after the script executes?

For a build process that requires updating from Node.js 12 to 14, I'd like a bash script to detect whether nvm is installed, and, if so, do nvm use v14 (or nvm install v14 if necessary), and then I want the nvm-selected node version to stick at 14 after the bash script terminates, not just for the duration of the script.
I can switch to v14 with this script, but after the script has terminated, the shell environment remains at v12:
#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
nvm --version
echo $NVM_BIN
node --version
nvm use v14
echo $NVM_BIN
node --version
Just executing the nvm command in a bash script is a pain because nvm isn't a true command, but a shell function, and the script has to use the first three lines to set up nvm for the script.
The output is:
0.33.11
/home/pi/.nvm/versions/node/v12.21.0/bin
v12.21.0
Now using node v14.16.0 (npm v6.14.11)
/home/pi/.nvm/versions/node/v14.16.0/bin
v14.16.0
When the script is finished, however:
I think the trick might be making sure the environment variable NVM_BIN persists at the v14 path when the script exits, but I don't know how to do that. In fact, I think it's generally not allowed for the shell in which a script executes to change environment variables in the parent shell.
The nvm commmand, however, is itself a shell script, and whatever it does is persistent after it's done executing. There should be some way for me to make this happen too.
I think if you set an alias with nvm for default,
nvm alias default 14.0.0
the node version will persist across any new terminal instances.
To have the selected node version after the script executes you can run it like this:
. change-node-version.sh
script
node --version
nvm use 14.18.0
node --version
then:
node --version
➜ v14.18.0

By restarting system terminal does not recognize npm and node commands

I have installed node and npm by nvm; At the end of instaling nvm, below message was shown:
Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
So I run that command and use nvm, install node versions and npm.
But when I restart my system, I have to run above command otherwise terminal says command not found.
What is the problem and how can I fix it?
The nvm installation should have added a couple of lines to your shell initialization script that makes sure to initialize nvm in every new shell, but for some reason this seems to have not happened.
Assuming that you're using bash, try adding these lines to your ~/.bash_profile
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
Then start a new terminal session and see if that does the trick.
Another thing that could be worth double-checking is to see if you have any installed node version aliased as the default one. I'm not sure what nvm does if you don't have a default version specified (if that's even possible). Run nvm ls and see you you have default pointing to any installed version of Node. If not, then set that up by running nvm alias default [version-number].

npm install zsh: command not found: npm

I just reinstalled node on my laptop and trying to install packaged in project. I installed node via brew and Im using MacOS.
npm install
zsh: command not found: npm
The solution below actually solved my issue
https://superuser.com/questions/1403007/zsh-npm-node-nvm-command-not-found-after-installing-ohmyzsh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
# This loads nvm bash_completion
this line of code on your .zshrc file to do that type vim ~/.zshrc than add the following lines in the bottom and to save the file click ctrl than press :wq! to save file in vim you can also use nano
Your binaries for npm are in /usr/local/share/npm/bin. Is this in your path environment variable?
If not export it at ~/.zshrc with export PATH=/usr/local/share/npm/bin:$PATH.
For me, I encountered the same issue as you did.
How I fixed:
First, checking whether npm is installed or not by checking its version:
npm --version
If the terminal still shows no npm not found, then type:
nvm install --lts
This command will trigger updating node package manager to the latest version.
Hope it helps.
You should run nvm use node every time you startup a new terminal.

How to change Node.js version with nvm

I'm using Yeoman to create a project. When I try to use Gulp.js I run the command gulp serve. An error tells me that I need an older version of Node.js (8.9.4), knowing that I've installed the latest version (10.14.1).
So I installed nvm to change the Node.js version. I had to set it into path C:\, and then I run with success: nvm install 8.9.4. And when I try to use it, nvm use 8.9.4, it’s always the latest version that is used:
If I try to use 8.10.0 and then run node -v, it tells me access refused, and the same to any Node.js command.
nvm install 8.10.0 is for installing proposed node version locally.
In order to use it:
nvm use 8.10.0
Note that you need to run this command as administrator.
You can always set default Node.js version:
nvm alias default 8.10.0
Install
nvm install 8.10.0
Use once per terminal
nvm use 8.10.0
Set up as the default for all terminals
nvm alias default 8.10.0
Set up Node.js version for your IDE
For more information check nvm documentation
Switch to a specific Node.js version
nvm use 8.10.0
Switch to the latest Node.js version
nvm use node
Switch to the latest LTS version
nvm use --lts
You can check which versions you have installed by running:
nvm ls
The entry in green, with an arrow on the left, is the current version in use.
Specify a Node.js version on a per-project basis
Version managers, such as RBEnv, allow you to specify a Ruby version on a per-project basis (by writing that version to a .ruby-version file in your current directory). This is kind of possible with nvm in that, if you create a .nvmrc file inside a project and specify a version number, you can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node.js you specify.
If it’s important to you that this happens automatically, there are a couple of snippets on the project’s home page for you to add to your .bashrc or .zshrc files to make this happen.
Here’s the Z shell (executable zsh) snippet. Place this below your nvm configuration:
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
When you change into a directory with a .nvmrc file, your shell will automatically change the Node.js version.
Make sure you run your terminal as an system administrator
nvm use <version> # This should work fine
Without the privilege, I was getting this error:
nvm use 16.14.0
Output:
exit status 5: Access is denied.
You need to edit your .bashrc file.
Add the below to that file. Change the version to your preferred version. This example uses v16.13.1.
It is possible you have something like this already in that file which causes the change back to your previous versions.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
export PATH="/home/zentech/.local/bin:/home/zentech/.nvm/versions/node/v14.18.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

Resources