fengyuanchen/cropperjs

Add type: "module" to package.json

オープン

#1,228 opened on 2025/03/02

 (2 件のコメント) (4 件のリアクション) (0 人の担当者)TypeScript (2,431 件のフォーク)batch import
help wantedquestion

Repository metrics

Stars
 (13,864 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

Cropper version

2.0.0

Link to minimal reproduction

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAYShMYCmU4DNkjgIgGNlUMArAZ3wG4AoWgGzXgA8AuRE9TAXjgDs0Ad04puACgAmEQgFcQafjAB0xNAEMYaAKJMFS8flABzfAEoz1IA

Steps to reproduce

Default-import cropperjs in a TypeScript project with "module": "Node16" or "module": "NodeNext" in tsconfig.json

What is expected?

TypeScript recognizes the default import as the Cropper class.

What is actually happening?

TypeScript recognizes the default import as a namespace (with {default: Cropper })

Any additional comments?

Can you add "type": "module" to the package.json?


Consider this code:

import Cropper from "cropperjs";
let x: Cropper = new Cropper(document.createElement("img"));

With module: "es2022" in the tsconfig.json it works. With module: "node16" it fails. Try this playground example and change the module resolution via TS Config -> Module.

https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAYShMYCmU4DNkjgIgGNlUMArAZ3wG4AoWgGzXgA8AuRE9TAXjgDs0Ad04puACgAmEQgFcQafjAB0xNAEMYaAKJMFS8flABzfAEoz1IA

The error is:

Cannot use namespace 'Cropper' as a type.

This expression is not constructable.

I.e. it expects the default imported Cropper to be {default: Cropper}.

This can be solved by setting "type": "module" in the package.json of cropperjs. I tried adding this via yarn patching cropperjs and can confirm it worked.

There are various reasons for setting module to node16 / nodenext, e.g. support for the exports field in the package.json

The only option for that type error is to access default manually, i.e.

import Cropper from "cropperjs";
let x: Cropper.default = new Cropper.default(document.createElement("img"));

But that's going to fail with pretty much every bundler out there, since they recognize it as an ESM module without "type": "module".

primefaces/primefaces#13456

コントリビューターガイド