fengyuanchen/cropperjs

Add type: "module" to package.json

Open

#1,228 建立於 2025年3月2日

在 GitHub 查看
 (2 留言) (4 反應) (0 負責人)JavaScript (12,111 star) (2,388 fork)batch import
help wantedquestion

描述

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

貢獻者指南

Add type: "module" to package.json · fengyuanchen/cropperjs#1228 | Good First Issue