fengyuanchen/cropperjs

Add type: "module" to package.json

Open

#1228 opened on Mar 2, 2025

View on GitHub
 (2 comments) (4 reactions) (0 assignees)JavaScript (12,111 stars) (2,388 forks)batch import
help wantedquestion

Description

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

Contributor guide