VSCode 使用 WSL 的 ESLint

ESLint on VSCode using WSL

我想在 Windows 上为 Node.js 设置我的开发环境,尽可能使用 Linux 的 Windows 子系统。

我已经在 WSL 和 Visual Studio 代码上安装了 Node.js 和 ESLint,在 Windows 上安装了 ESLint 扩展。

但是当我打开 VSCode 时,我从 ESLint 扩展中收到此消息错误:

Failed to load the ESLint library for the document c:\PATH\TO\FILE.js
To use ESLint for single JavaScript file install eslint globally using 'npm install -g eslint'.
You need to reopen VS Code after installing eslint.

让它工作的唯一方法是也为 Windows 安装 Node.js(和 ESLint)?

有一种方法可以完成这项工作。

在 WSL 中安装 nodejs 后,使用以下命令设置 NPM:

mkdir ~/.npm-global
npm config set prefix ~/.npm-global
echo 'PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.profile
. ~/.profile

如果您使用 Ubuntu 作为 WSL 发行版,请确保您已在 WSL 中全局安装了 eslint:

npm install -g eslint

接下来,将此设置添加到 VSCode:

"eslint.nodePath": "C:/Users/YOURUSERNAME/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/rootfs/home/UBUNTUUSERNAME/.npm-global/lib/node_modules"

确保用您的 Windows 用户名和 Ubuntu 用户名替换路径。这将 VSCode 指向您在 WSL 中的 eslint 位置。