Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No exported names found in module 'prettier' in an ESM project #3101

Open
andersk opened this issue Nov 13, 2024 · 5 comments
Open

No exported names found in module 'prettier' in an ESM project #3101

andersk opened this issue Nov 13, 2024 · 5 comments

Comments

@andersk
Copy link
Contributor

andersk commented Nov 13, 2024

In an ESM project (.mjs or "type": "module"), eslint-plugin-import can’t find any exports in the prettier package. On import * as prettier from "prettier";, the import/namespace rule complains “No exported names found in module 'prettier'”. Similarly, on import prettier from "prettier";, the import/default rule complains ‘No default export found in imported module "prettier"’.

Complete reproducible example:

eslint.config.mjs

import eslintImport from "eslint-plugin-import";

export default [
  eslintImport.flatConfigs.recommended,
  { languageOptions: { ecmaVersion: "latest" } },
];

foo.mjs

import * as prettier from "prettier";

(async () => {
  console.log(await prettier.format("x=1", { filepath: "test.js" }));
})();
$ npm i eslint eslint-plugin-import prettier

$ npm list
test@ /tmp/test
├── [email protected]
├── [email protected]
└── [email protected]

$ node foo.mjs
x = 1;

$ npx eslint foo.mjs

/tmp/test/foo.mjs
  1:8   error  No exported names found in module 'prettier'         import/namespace
  4:30  error  'format' not found in imported namespace 'prettier'  import/namespace

✖ 2 problems (2 errors, 0 warnings)
@ljharb
Copy link
Member

ljharb commented Nov 13, 2024

This plugin uses resolve, which doesn't yet support the exports field.

prettier's main points to https://unpkg.com/[email protected]/index.cjs, which is a CJS file with a single default export.

In other words, as far as the plugin's concerned, you'd need to import prettier from 'prettier'. In node, the native import would point to https://unpkg.com/browse/[email protected]/index.mjs, which seems to support that same pattern (it would also work with import * as, of course)

What happens if you change how you're importing it?

@andersk
Copy link
Contributor Author

andersk commented Nov 13, 2024

I tried that above:

Similarly, on import prettier from "prettier";, the import/default rule complains ‘No default export found in imported module "prettier"’.

$ npx eslint foo.mjs

/tmp/test/foo.mjs
  1:8  error  No default export found in imported module "prettier"  import/default

✖ 1 problem (1 error, 0 warnings)

@ljharb
Copy link
Member

ljharb commented Nov 13, 2024

aha, well then that's certainly not a viable workaround :-)

@andersk
Copy link
Contributor Author

andersk commented Nov 13, 2024

Also, the plugin does not complain about import * as on other CJS-only modules. (At runtime, such an import causes Node to create a namespace wrapper with a default key, and possibly other keys via cjs-module-lexer.)

@ljharb
Copy link
Member

ljharb commented Nov 13, 2024

It's likely some complex interaction with the combination of main, module, exports, and ESM-transpiled output used in prettier specifically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants