YouCompleteMe ERROR: Unable to find executable 'npm'. npm is required to install TSServer

YouCompleteMe ERROR: Unable to find executable 'npm'. npm is required to install TSServer

在为 vim 安装 YouCompleteMe 时,我想安装对 Java 和 Java 脚本的支持。按照说明 from here,我因此 运行:

sudo /usr/bin/python3.6 ./install.py  --java-completer --ts-completer

但是,它会出错并显示以下消息:

...
[100%] Linking C shared library /home/vagrant/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/cregex/regex_3/_regex.so
[100%] Built target _regex
Downloading jdt.ls from http://download.eclipse.org/jdtls/snapshots/jdt-language-server-0.54.0-202004152304.tar.gz...
Extracting jdt.ls to /home/vagrant/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/eclipse.jdt.ls/target/repository...
Done installing jdt.ls
ERROR: Unable to find executable 'npm'. npm is required to install TSServer.

这是可以理解的,除非我已经在 PATH 上设置了 npm,如下所示:

=> npm -v
6.13.4
=> which npm
/home/vagrant/.nvm/versions/node/v8.17.0/bin/npm

关于为什么支持 ts-completer 的 YouCompleteMe 安装失败的任何猜测?

Any guesses on why YouCompleteMe with ts-completer support is failing to install?

安装失败,因为你运行宁install.py为sudonvm安装节点的位置不在secure_path。当脚本 运行 为 sudo 时使用 secure_path

你可以看到PATHsecure_path的不同内容是这样的:

$ echo 'echo $PATH' | sudo sh    # secure_path
$ echo 'echo $PATH' | sh         # your user PATH

您还可以确认 sudo 在其路径上缺少 npm,如下所示:

$ which npm
$ sudo which npm

怎么办?

一个选项是 运行 不带 sudo 的脚本,如下所示:

/usr/bin/python3.6 ./install.py  --java-completer --ts-completer

另一个是将 nvm bin 内容复制到 secure_path 位置之一。 /usr/local/bin 就是其中之一。

$ find $(which npm | xargs dirname) -type l -o -type f | sudo xargs cp -t /usr/local/bin

# test
$ sudo npm -v
$ sudo node -v

之后,安装程序会找到 npm