import-js/eslint-plugin-import

`import/namespace` creates false positives for 'prettier' in v2.30.0

marikaner opened this issue ยท 12 comments

Hey, I am using prettier within a generator cli. I am trying to upgrade to version 2.30.0 but it shows me the following error when I do:
import * as prettier from 'prettier';: No exported names found in module 'prettier'..
When I import it with import { format } from 'prettier' it does not complain, proving that the name actually exists.

I am using prettier version 3.3.3 with CJS.

hmm, i wonder if this is caused by ExportMap changes (cc @soryy708)

I don't know if it helps, but it also reports for import prettier from 'prettier'; with esModuleInterop enabled for the rule import/default. I assume the same root cause.

esModuleInterop must always be enabled, or else TS's module system is broken.

Is this a regression or is this a configuration error in relation to esModuleInterop?

Since esModuleInterop only relates to tsconfig, and isn't part of this config at all, it seems like it'd be a regression.

@marikaner Is this about a TypeScript project? or just JavaScript? If TypeScript, what versions of @typescript-eslint/parser and eslint-import-resolver-typescript?

Oh yes, it is a TypeScript project. We do not have these dependencies and now I wonder how this ever worked. After I added them we have a lot of other (legit) findings, but this one is gone. Thanks for your support!

This should be reopened, as it can be reproduced in a plain JavaScript project with ES modules:

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" }));
})();
$ 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)

@andersk TS's module system is broken unless you're using esModuleInterop and synthetic imports. If you do that, what happens?

@ljharb There is no TS at all in my example.

ah, i saw import * as and assumed TS, since there's not much reason to use that syntax otherwise. i believe it should work the same if you import prettier from 'prettier'?

Either way, if it's still a problem, please file a new issue and I'll do my best to look into it.

In general, a namespace import (import * as m) is only equivalent to a default import (import m) when importing a CommonJS module. Some ESM packages arrange to export everything both ways (perhaps for backwards compatibility with previous CommonJS versions) but that is not required.

In any case, I get errors both ways, as detailed in my new issue: