nodeenv icon indicating copy to clipboard operation
nodeenv copied to clipboard

ERR_MODULE_NOT_FOUND when using imports in an eslint config file

Open bmispelon opened this issue 1 year ago • 1 comments

Hi,

I've been encountering a strange error when using eslint and I think (not 100% sure) that the issue might come from nodeenv.

I originally encountered the problem while using pre-commit (which uses nodeenv internally) but I was able to trim down the setup to a single file:

  1. Create a new virtual environment: nodeenv env
  2. Activate it: . env/bin/activate
  3. Install eslint and globals (globally): npm install -g eslint globals
  4. Create a config file for eslint: echo 'import globals from "globals"; export default [{languageOptions: { globals: globals.browser }}];' > eslint.config.mjs
  5. Run eslint: eslint .

On my computer, this results in a crash with the following error:

Oops! Something went wrong! :(

ESLint: 9.2.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'globals' imported from /path/to/project/eslint.config.mjs
Did you mean to import "globals/index.js"?
    at packageResolve (node:internal/modules/esm/resolve:845:9)
    at moduleResolve (node:internal/modules/esm/resolve:918:18)
    at defaultResolve (node:internal/modules/esm/resolve:1148:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:541:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

As a workaround, I can make the error go away by installing globals locally: npm install globals (with the virtual environment still active). After that running eslint . doesn't crash anymore.

Is there something that nodeenv is not handling correctly here, or is the issue somewhere further down the line?

Thanks! ✨

bmispelon avatar May 09 '24 12:05 bmispelon

I think this is a behaviour change in ESLint 9, it happens under npx without using nodeenv. I made a report on ESLint: https://github.com/eslint/eslint/issues/18465 .

adamchainz avatar May 17 '24 11:05 adamchainz