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

ESM Import Failing - v6.0.0 #189

Closed
dscalzi opened this issue Jan 15, 2023 · 17 comments
Closed

ESM Import Failing - v6.0.0 #189

dscalzi opened this issue Jan 15, 2023 · 17 comments

Comments

@dscalzi
Copy link

dscalzi commented Jan 15, 2023

minimatch - 6.0.0
typescript - 4.9.4
node - 16.17.0

Error:

src/structure/spec_model/module/Module.struct.ts:218:59 - error TS2349: This expression is not callable.
  Type 'typeof import("F:/Project/node_modules/minimatch/dist/cjs/index")' has no call signatures.

218         return this.untrackedFilePatterns.find(pattern => minimatch(pathRelativeToContainer, pattern))

Usage:

import minimatch from 'minimatch'

protected isFileUntracked(pathRelativeToContainer: string): string | undefined {
    return this.untrackedFilePatterns.find(pattern => minimatch(pathRelativeToContainer, pattern))
}

tsconfig

{
  "compilerOptions": {
    "target": "ES2021",
    "module": "Node16",
    "lib": ["ES2021"],
    "sourceMap": true,
    "outDir": "dist",
    "moduleResolution": "Node16",
    "esModuleInterop": true
  }
}
@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

What version of typescript are you using? This works for me:

$ npm ls
foo@ /Users/isaacs/dev/isaacs/foo
├── [email protected]
└── [email protected]
import minimatch from 'minimatch'

export class Test {
  untrackedFilePatterns: string[] = ['a', 'b', 'c']
  protected isFileUntracked(
    pathRelativeToContainer: string
  ): string | undefined {
    return this.untrackedFilePatterns.find(pattern =>
      minimatch(pathRelativeToContainer, pattern)
    )
  }
}

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

Oh, you said you're using 4.9.4. Then I wonder what's going on there...?

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

Aha, I was able to reproduce it. This is weird.

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

In the short term, you can import { minimatch } from 'minimatch' and it'll work fine. It seems to be some confusion with the default export also being exported as a named export, and specific combination of tsconfig options here. I'll update the readme to make import { minimatch } the suggested inclusion mechanism.

@dscalzi
Copy link
Author

dscalzi commented Jan 15, 2023

That worked 👍

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

It's really weird, like, I see this in the d.ts:

export declare const minimatch: {
    // v-- THIS IS A CALL SIGNATURE, WAT!?
    (p: string, pattern: string, options?: MinimatchOptions): boolean;
    sep: string;
    GLOBSTAR: typeof GLOBSTAR;
    filter: (pattern: string, options?: MinimatchOptions) => (p: string) => boolean;
    defaults: (def: MinimatchOptions) => typeof minimatch;
    braceExpand: (pattern: string, options?: MinimatchOptions) => string[];
    makeRe: (pattern: string, options?: MinimatchOptions) => false | MMRegExp;
    match: (list: string[], pattern: string, options?: MinimatchOptions) => string[];
    Minimatch: typeof Minimatch;
};
// v-- IT IS THE DEFAULT EXPORT! THE THING WITH THE CALL SIGNATURE!
export default minimatch;

but then:

This expression is not callable.
  Type 'typeof import("/Users/isaacs/dev/isaacs/foo/node_modules/minimatch/dist/cjs/index")' has no call signatures. (tsserver 2349)

@dscalzi
Copy link
Author

dscalzi commented Jan 15, 2023

Possibly a typescript bug, I know it took them a significant effort to support ESM in the first place.

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

Aha, if you use "moduleResolution": "node" instead of "node16" then it works. Not sure what the difference is there.

@dscalzi
Copy link
Author

dscalzi commented Jan 15, 2023

That disables ESM and uses commonjs https://www.typescriptlang.org/tsconfig#node16nodenext-nightly-builds

@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

Ok, this gets even stranger. After experimenting with it a bit and trying to work out a minimal test case, now I can't reproduce the error, even starting from the exact same setup where I was reproducing it before. This definitely feels like a strangely non-deterministic TS bug. I'm going to update the readme and call it fixed 😂

@dscalzi
Copy link
Author

dscalzi commented Jan 15, 2023

Works for me, I prefer names imports anyway haha

@isaacs isaacs closed this as completed in 21b846e Jan 15, 2023
isaacs added a commit to isaacs/rimraf that referenced this issue Jan 15, 2023
@isaacs
Copy link
Owner

isaacs commented Jan 15, 2023

This is the TS issue: microsoft/TypeScript#50466

"Proper" (ugly) fix incoming.

@isaacs isaacs reopened this Jan 15, 2023
@isaacs isaacs closed this as completed in d3fdd61 Jan 15, 2023
@redonkulus
Copy link

Just to follow up here, I tried using const minimatch = require('minimatch'); and that failed as well. I had to switch to const { minimatch } = require('minimatch'); for it to work again.

@isaacs
Copy link
Owner

isaacs commented Jan 22, 2023

@redonkulus What version of minimatch? Should be fixed on latest.

@redonkulus
Copy link

I tried this morning on the latest version from npm.

isaacs added a commit that referenced this issue Jan 22, 2023
@isaacs
Copy link
Owner

isaacs commented Jan 22, 2023

@redonkulus Ah, indeed. Updated "main" but not exports['.'].require, sorry about that.

Fixed on a4fa1f8, published 6.1.6

mahula added a commit to Ocelot-Social-Community/Ocelot-Social that referenced this issue Sep 26, 2023
@CodeLittlePrince
Copy link

import { minimatch } from "minimatch";
It works for me.

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

No branches or pull requests

4 participants