Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nvm use in vscode, run npm script and switch node versions automatically #3385

Closed
vanchKong opened this issue Jul 4, 2024 · 5 comments
Closed

Comments

@vanchKong
Copy link

vanchKong commented Jul 4, 2024

Operating system and version:

nvm debug output:

nvm --version: v0.39.7
$TERM_PROGRAM: vscode
$SHELL: /bin/zsh
$SHLVL: 1
whoami: 'vanch'
${HOME}: /Users/vanch
${NVM_DIR}: '${HOME}/.nvm'
${PATH}: ${NVM_DIR}/versions/node/v12.22.12/bin:${NVM_DIR}/versions/node/v20.15.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:${NVM_DIR}/versions/node/v20.15.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin:${HOME}/apache-maven-3.9.6/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:${HOME}/apache-maven-3.9.6/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools
$PREFIX: ''
${NPM_CONFIG_PREFIX}: ''
$NVM_NODEJS_ORG_MIRROR: ''
$NVM_IOJS_ORG_MIRROR: ''
shell version: 'zsh 5.9 (x86_64-apple-darwin23.0)'
uname -a: 'Darwin 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64'
checksum binary: 'shasum'
OS version: macOS 14.5 23F79
awk: /usr/bin/awk, awk version 20200816
curl: /usr/bin/curl, curl 8.6.0 (x86_64-apple-darwin23.0) libcurl/8.6.0 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.12 nghttp2/1.61.0
wget: not found
sed: /usr/bin/sed
cut: /usr/bin/cut
basename: /usr/bin/basename
rm: /bin/rm
mkdir: /bin/mkdir
xargs: /usr/bin/xargs
git: /usr/bin/git, git version 2.39.3 (Apple Git-146)
grep: /usr/bin/grep, grep (BSD grep, GNU compatible) 2.6.0-FreeBSD
nvm current: v12.22.12
which node: ${NVM_DIR}/versions/node/v12.22.12/bin/node
which iojs: iojs not found
which npm: ${NVM_DIR}/versions/node/v12.22.12/bin/npm
npm config get prefix: ${NVM_DIR}/versions/node/v12.22.12
npm root -g: ${NVM_DIR}/versions/node/v12.22.12/lib/node_modules

nvm ls output:

->    v12.22.12
       v16.20.2
       v20.15.0
default -> 20 (-> v20.15.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.15.0) (default)
stable -> 20.15 (-> v20.15.0) (default)
lts/* -> lts/iron (-> v20.15.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2
lts/hydrogen -> v18.20.3 (-> N/A)
lts/iron -> v20.15.0

How did you install nvm?

install script in readme

What steps did you perform?

  • ~/.zshrc has command to automatically switch node versions
  • Created a .nvmrc file in the project, containing the non-default version 12

What happened?

  • Open the project with vscode, open the terminal in vscode, and the node version switch automatically
  • Click run in the npm script that comes with vscode, and the default node version is still used
image

What did you expect to happen?

  • Click run in the npm script that comes with vscode, and the node version switch automatically

Is there anything in any of your profile files that modifies the PATH?

no

@vanchKong
Copy link
Author

~/.zshrc

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm


# place this after nvm initialization!
autoload -U add-zsh-hook

load-nvmrc() {
  local nvmrc_path
  nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version
    nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
      nvm use
    fi
  elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}

add-zsh-hook chpwd load-nvmrc
load-nvmrc

@ljharb
Copy link
Member

ljharb commented Jul 4, 2024

Is "npm scripts" a feature built into vscode, or a plugin? Either way I think you need to file an issue there; if the vscode terminal is correct, then anything that launches a program should start in that terminal, and also be correct.

Even the autoload scripts aren't part of nvm; you're kind of on your own when you choose to use them.

@vanchKong
Copy link
Author

got it

@ljharb ljharb closed this as not planned Won't fix, can't repro, duplicate, stale Jul 4, 2024
@vanchKong
Copy link
Author

sorry,again。
move config about nvm in ~/.zshrc to ~/.zprofile, my expect will happen, just for who have this problem later.

@ljharb
Copy link
Member

ljharb commented Jul 4, 2024

@vanchKong gotcha, that means that the "run script" command isn't starting a login shell. It's still something you should file with vscode or the extension.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants