sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Error importing sdk from nodejs in module env

Open farwayer opened this issue 2 years ago • 4 comments

sdk 6.3.1
node v20.6.1

package.json:

{
  "type": "module"
}

index.js:

import dock from '@docknetwork/sdk'

errors:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'node_modules/@docknetwork/crypto-wasm-ts/lib/anonymous-credentials/presentation' imported from node_modules/@docknetwork/sdk/index.js
Did you mean to import @docknetwork/crypto-wasm-ts/lib/anonymous-credentials/presentation.js?
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'node_modules/@docknetwork/crypto-wasm-ts/lib/anonymous-credentials' is not supported resolving ES modules imported from node_modules/@docknetwork/sdk/index.js
Did you mean to import @docknetwork/crypto-wasm-ts/lib/anonymous-credentials/index.js?

...

farwayer avatar Sep 24 '23 10:09 farwayer

That's an odd error, does node_modules/@docknetwork/sdk/node_modules/@docknetwork/crypto-wasm-ts exist?

cykoder avatar Sep 28 '23 23:09 cykoder

The problem is the file extension is missed. And directory import is not allowed in module environment. See https://nodejs.org/api/esm.html#mandatory-file-extensions

Look like you are using some bundler (rollup?) in your projects and it hides and fixes this error.

It's very easy to reproduce:

mkdir test && cd test
echo '{"type":"module","dependencies":{"@docknetwork/sdk": "^6.5.1"}}' > package.json
echo 'import dock from "@docknetwork/sdk"' > index.js
npm install
node index.js

farwayer avatar Sep 29 '23 10:09 farwayer

I would suggest to use babel, as even if the sdk itself as fully node module compatible there is no guarantee all child packages would be which can cause this error

cykoder avatar Sep 29 '23 15:09 cykoder

I still get this error with the latest version, what is the official method to get sdk working on nodejs?

dcolley avatar Apr 15 '24 10:04 dcolley