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

Type checking errors when requiring @hashgraph/sdk #2722

Open
acuarica opened this issue Dec 16, 2024 · 0 comments
Open

Type checking errors when requiring @hashgraph/sdk #2722

acuarica opened this issue Dec 16, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@acuarica
Copy link

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

@acuarica acuarica added the bug Something isn't working label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant