iotaledger/iota.rs

[Bug]: add iota.rs nodejs module to new project after a building locally

Closed this issue · 3 comments

Issue description

Hi,

I succfully built the iota.rs node binding on a raspberry board.
But can't use it whitin new node project.

Version

branch : develop

Expected behaviour

I expect to add the node module to my node project using yarn add file:/home/iota/iota.rs/client/bindings/nodejs/'

Actual behaviour

can't add the nodejs binding to a new node projet

Can the issue reliably be reproduced?

Yes

Steps to reproduce the issue

cd iota.rs/
cargo build --release
cd iota.rs/client/bindings/nodejs
cargo build --release
yarn install
cd empty_project
npm init
yarn add iota.rs/client/bindings/nodejs

Errors

$ yarn add file:/home/iota/iota.rs/client/bindings/nodejs/
yarn add v1.22.19
warning package.json: "test" is also the name of a node core module
info No lockfile found.
warning test@1.0.0: "test" is also the name of a node core module
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
error /home/iota/test/node_modules/@iota/client: Command failed.
Exit code: 101
Command: prebuild-install --tag-prefix nodejs-binding-v || npm run rebuild
Arguments: 
Directory: /home/iota/test/node_modules/@iota/client
Output:
prebuild-install warn install No prebuilt binaries found (target=18.13.0 runtime=node arch=arm64 libc= platform=linux)

> @iota/client@3.0.0-rc.5 rebuild
> npm run build:neon && tsc && node scripts/node-neon-build.js --strip && node scripts/strip.js


> @iota/client@3.0.0-rc.5 build:neon
> cargo-cp-artifact -nc ./index.node -- cargo build --release --message-format=json-render-diagnostics

error: failed to load manifest for dependency `iota-client`

Caused by:
  failed to read `/home/iota/test/node_modules/Cargo.toml`

Caused by:
  No such file or directory (os error 2)
Did not copy "cdylib:client"
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Duplicate declaration

  • I have searched the issues tracker this issue and there is none

Code of Conduct

  • I agree to follow this project's Code of Conduct

After checking https://github.com/iotaledger/iota.rs/blob/e888aa8a8a16c7d95743e87a02299f3fb886d256/.github/workflows/covector.yml

I see that I have to run

yarn prebuild:node

that generates the build file

prebuild info build Prebuild written to prebuilds/@iota/client-v3.0.0-rc.5-node-v108-linux-arm64.tar.gz

Then when adding using yarn

$ yarn add file:client-v3.0.0-rc.5-node-v108-linux-arm64.tar.gz 
yarn add v1.22.19
warning package.json: "test" is also the name of a node core module
info No lockfile found.
warning test@1.0.0: "test" is also the name of a node core module
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
warning test@1.0.0: "test" is also the name of a node core module
success Saved 1 new dependency.
info Direct dependencies
└─ client-v3.0.0-rc.5-node-v108-linux-arm64@0.0.0
info All dependencies
└─ client-v3.0.0-rc.5-node-v108-linux-arm64@0.0.0
Done in 1.07s.

But when running the sample code, I got this error

(node:139410) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/iota/empty_project/index.ts:4
import { Client, initLogger } from '@iota/client';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:74:18)
    at wrapSafe (node:internal/modules/cjs/loader:1141:20)
    at Module._compile (node:internal/modules/cjs/loader:1182:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.13.0

Any help, please!

To load an ES module, set "type": "module" in the package.json

Have you tried this?

Thank you for the reply.

In fact, there is more than one issue.

First, when using the generated file using yarn prebuild:node , I can't call the iota module.
That creates *.node file node_modules/client-v3.0.0-rc.5-node-v108-linux-arm64/Release/index.node. I'm not certain if that's a correct node_modules path.

So, I did otherwise, using npm link to create a global link to the built nodejs client.
I can check that :

npm list -g --depth 0
/home/iota/.nvm/versions/node/v18.13.0/lib
├── @iota/client@3.0.0-rc.5 -> ./../../../../../iota.rs/client/bindings/nodejs
├── corepack@0.15.2
├── npm@8.19.3
└── yarn@1.22.19

Then I add the link to the project. From the project folder:

npm link @iota/client

At this moment I can see node_modules/@iota/client/..

After all that, I still see the above error

To load an ES module, set "type": "module" in the package.json

But, that for using the project as a module. It's not the case.
I have just to change to

const { Client, initLogger } = require('@iota/client')

Maybe it's not the best way to embedd nodejs client. But that's works.
Can you please add the best instructions to the readme file.

By the way I also compiled the python client and I'm using the generated wheel !

Thank you.