Skip to content

Type checking errors when requiring @hashgraph/sdk #2722

@acuarica

Description

@acuarica

Description

When loading the SDK with require('@hashgraph/sdk'), I get a bunch of errors like

node_modules/@hashgraph/sdk/lib/Transfer.d.ts:75:20 - error TS1340: Module 'long' does not refer to a type, but is used as a type here. Did you mean 'typeof import('long')'?

75 export type Long = import("long");
                      ~~~~~~~~~~~~~~

node_modules/long/umd/index.d.ts:1:18 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("../index.js")' call instead.

1 import Long from "../index.js";

and the error

scripts/create-token.js:21:9 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@hashgraph/sdk")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mjs', or add the field `"type": "module"` to 'package.json'.

21 require('@hashgraph/sdk');
           ~~~~~~~~~~~~~~~~

Steps to reproduce

In a new npm JavaScript project, add the SDK 2.55.1 and typescript. The package.json's type is left untouched (it's using commonjs as default.

Use the following tsconfig.json

{
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "module": "Node16",
        "moduleResolution": "Node16",
        "resolveJsonModule": true,
        "noEmit": true,
        "strict": true,
        "exactOptionalPropertyTypes": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitAny": true,
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noPropertyAccessFromIndexSignature": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
    },
}

Then create a minimal script that requires @hashgraph/sdk, for example

require('@hashgraph/sdk');

The errors mentioned above appear when type-checking

npx tsc 

Additional context

If, however, I set skipLibCheck: true in tsconfig.json, I get only the following error mentioned above (errors Module 'long' does not refer to a type disappear)

$ npx tsc
scripts.js:21:9 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@hashgraph/sdk")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mjs', or add the field `"type": "module"` to 'package.json'.

21 require('@hashgraph/sdk');
           ~~~~~~~~~~~~~~~~

The last version of the SDK is installed

$ npm why @hashgraph/sdk
@hashgraph/[email protected] dev
node_modules/@hashgraph/sdk
  dev @hashgraph/sdk@"^2.55.1" from the root project

Hedera network

other

Version

2.55.1

Operating system

macOS

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

Status

📋 Backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions