microsoft/vscode-eslint

ESLint doesn't resolve paths to packages installed globally

romanstetsyk opened this issue · 2 comments

I'm developing inside a Docker container, therefore there's no node_modules directory on the host. The flat config that I'm using has imports from @eslint/js and globals packages. They are installed globally on the host machine for the purpose of ESLint extension. The eslint itself is installed globally too.

$ npm ls -g                  
/Users/myusername/.local/share/mise/installs/node/22.6.0/lib
├── @eslint/js@9.9.0
├── corepack@0.29.3
├── eslint@9.9.0
├── globals@15.9.0
└── npm@10.8.2
// eslint.config.js
import pluginJs from "@eslint/js";
import globals from "globals";

export default [
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
];
// .vscode/settings.json
{
  "eslint.runtime": "/Users/myusername/.local/share/mise/installs/node/22.6.0/bin/node",
  "eslint.nodePath": "/Users/myusername/.local/share/mise/installs/node/22.6.0/lib/node_modules",
  "eslint.debug": true,
  "eslint.trace.server": "off",
  "eslint.useFlatConfig": true,
  "eslint.validate": ["javascript","javascriptreact","typescript","typescriptreact","html","vue","markdown"],
  "eslint.workingDirectories": [{ "mode": "auto" }]
}

The error I'm getting is:

[Info  - 14:27:10] ESLint server is starting.
[Info  - 14:27:10] ESLint server running in node v22.6.0
[Info  - 14:27:10] ESLint server is running.
[Info  - 14:27:10] ESLint library loaded from: /Users/myusername/.local/share/mise/installs/node/22.6.0/lib/node_modules/eslint/lib/api.js
2024-08-21T13:27:11.000Z eslint:eslint Searching for eslint.config.js
2024-08-21T13:27:11.005Z eslint:eslint Loading config from /Users/myusername/test/eslintdocker/eslint.config.js
2024-08-21T13:27:11.005Z eslint:eslint Config file URL is file:///Users/myusername/test/eslintdocker/eslint.config.js
[Error - 14:27:11] An unexpected error occurred:
[Error - 14:27:11] Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@eslint/js' imported from /Users/myusername/test/eslintdocker/eslint.config.js
    at packageResolve (node:internal/modules/esm/resolve:839:9)
    at moduleResolve (node:internal/modules/esm/resolve:908:18)
    at defaultResolve (node:internal/modules/esm/resolve:1039:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:554:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:523:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

It looks like the ESLint extension doesn't resolve paths to packages installed globally, and "eslint.nodePath" in settings.json doesn't help. So my questions are: what am I doing wrong? How can I use the ESLint extension without installing eslint and everything that is imported in eslint.config.js locally in my working directory on the host?

I've tested it with ESLint extensions v3.0.10 and v3.0.13 (pre-release).

I get exactly the same error when executing eslint in the terminal:

eslint test.js 

Oops! Something went wrong! :(

ESLint: 9.9.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@eslint/js' imported from /workspaces/typescript-node/eslint.config.js
    at packageResolve (node:internal/modules/esm/resolve:839:9)
    at moduleResolve (node:internal/modules/esm/resolve:908:18)
    at defaultResolve (node:internal/modules/esm/resolve:1039:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:554:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:523:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

So this is not specific to the extension. ESLint itself can't find the package.

In general things don't work in the extension if they don't work in the terminal. I would recommend to ask on the ESLint repository why this is not working.

I will close the issue. Please ping if things are still not working in the extension even though they are working in the terminal.

I'm developing inside a Docker container, therefore there's no node_modules directory on the host. The flat config that I'm using has imports from @eslint/js and globals packages. They are installed globally on the host machine for the purpose of ESLint extension. The eslint itself is installed globally too.

$ npm ls -g                  
/Users/myusername/.local/share/mise/installs/node/22.6.0/lib
├── @eslint/js@9.9.0
├── corepack@0.29.3
├── eslint@9.9.0
├── globals@15.9.0
└── npm@10.8.2
// eslint.config.js
import pluginJs from "@eslint/js";
import globals from "globals";

export default [
  { languageOptions: { globals: globals.browser } },
  pluginJs.configs.recommended,
];
// .vscode/settings.json
{
  "eslint.runtime": "/Users/myusername/.local/share/mise/installs/node/22.6.0/bin/node",
  "eslint.nodePath": "/Users/myusername/.local/share/mise/installs/node/22.6.0/lib/node_modules",
  "eslint.debug": true,
  "eslint.trace.server": "off",
  "eslint.useFlatConfig": true,
  "eslint.validate": ["javascript","javascriptreact","typescript","typescriptreact","html","vue","markdown"],
  "eslint.workingDirectories": [{ "mode": "auto" }]
}

The error I'm getting is:

[Info  - 14:27:10] ESLint server is starting.
[Info  - 14:27:10] ESLint server running in node v22.6.0
[Info  - 14:27:10] ESLint server is running.
[Info  - 14:27:10] ESLint library loaded from: /Users/myusername/.local/share/mise/installs/node/22.6.0/lib/node_modules/eslint/lib/api.js
2024-08-21T13:27:11.000Z eslint:eslint Searching for eslint.config.js
2024-08-21T13:27:11.005Z eslint:eslint Loading config from /Users/myusername/test/eslintdocker/eslint.config.js
2024-08-21T13:27:11.005Z eslint:eslint Config file URL is file:///Users/myusername/test/eslintdocker/eslint.config.js
[Error - 14:27:11] An unexpected error occurred:
[Error - 14:27:11] Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@eslint/js' imported from /Users/myusername/test/eslintdocker/eslint.config.js
    at packageResolve (node:internal/modules/esm/resolve:839:9)
    at moduleResolve (node:internal/modules/esm/resolve:908:18)
    at defaultResolve (node:internal/modules/esm/resolve:1039:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:554:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:523:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

It looks like the ESLint extension doesn't resolve paths to packages installed globally, and "eslint.nodePath" in settings.json doesn't help. So my questions are: what am I doing wrong? How can I use the ESLint extension without installing eslint and everything that is imported in eslint.config.js locally in my working directory on the host?

I've tested it with ESLint extensions v3.0.10 and v3.0.13 (pre-release).

I have a a similar issue with another package, a Sentry package for Next.js (@sentry/nextjs). I am clueless what to do actually, this is the only issue on Google that seems to have the same stacktrace as me... Wow!