/usr/bin/env: ‘node’: 仅在 VSC 中没有这样的文件或目录

问题描述 投票:0回答:1

我有一个带有 Husky git 预提交挂钩的项目:

#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged

如果我从终端提交,挂钩和提交工作正常。

如果我尝试从 VSC 提交,则会收到错误:

> git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
/usr/bin/env: ‘node’: No such file or directory
husky - pre-commit hook exited with code 127 (error)
husky - command not found in PATH=/usr/lib/git-core:/home/myname/.cargo/bin:/home/myname/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

所以我一直试图弄清楚为什么 VSC 似乎不知道 nvm 以及当前节点可执行文件在哪里。

我的

~/.bashrc
致电
source ~/code/.bashrc_extra
,其中包括:

echo $0
which bash
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
nvm current
node -v

当我打开终端时,我看到:

bash
/usr/bin/bash
/home/myname/.nvm/versions/node/v18.18.2/bin/node
v18.18.2
v18.18.2

所以,终端中的一切都很好,所以它在那里工作是有道理的。

当我打开 VSC 终端时,我会看到相同的值,除了

/usr/bin/bash
而不是
bash

如何修复我的设置以便预提交挂钩能够工作?

  • VSC 1.84.2
  • Ubuntu 22.04.3
node.js bash ubuntu nvm husky
1个回答
0
投票

运行预提交挂钩时,Visual Studio Code (VSC) 似乎没有选择您的 NVM 环境。这可能是因为 VSC 终端没有作为交互式 shell 启动,因此它可能不会获取您的

~/.bashrc
和相关文件。

您可以尝试执行以下几个步骤来解决此问题

  1. 更新 VSC 设置:

在 Visual Studio Code 中打开设置(Ctrl + ,),然后单击“打开设置 (JSON)”图标(设置选项卡的右上角)。添加或修改以下设置:

"terminal.integrated.shell.linux": "/bin/bash",

此设置可确保 VSC 使用 Bash 作为默认 shell。

  1. 重新启动VSC

更改设置后,重新启动 Visual Studio Code 以应用更改。

  1. 使用
    .bash_profile
    代替
    .bashrc
    :

某些系统使用

.bash_profile
而不是
.bashrc
进行初始化。您可以尝试将 NVM 初始化代码从
.bashrc
移至
.bash_profile
:

# Add these lines to ~/.bash_profile
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

确保在启动非登录交互式 shell 时(例如在 VSC 中打开终端时)获取

.bash_profile

  1. 使用
    .bashrc
    代替
    Non-Login Shells

如果您希望将配置保留在

.bashrc
中,请确保您的
.bash_profile
包含它。例如:

# Add these lines to ~/.bash_profile
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi
© www.soinside.com 2019 - 2024. All rights reserved.