tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
431 stars 272 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module secp256k1/index.js from TronWeb.node.js #500

Closed sinawic closed 5 months ago

sinawic commented 5 months ago

hey, I'm getting this really bothering error I can't find a way to bypass when I simply install tronweb and importing it in my project

node -v v20.12.0 npm -v 10.5.0 uname -a Linux sinawic 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

full error:

node:internal/modules/cjs/loader:1424
      throw err;
      ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/@noble/secp256k1/index.js from /node_modules/tronweb/dist/TronWeb.node.js not supported.
Instead change the require of index.js in /node_modules/tronweb/dist/TronWeb.node.js to a dynamic import() which is available in all CommonJS modules.
    at /node_modules/tronweb/dist/TronWeb.node.js:1:296544
    at /node_modules/tronweb/dist/TronWeb.node.js:1:697113
    at Object.<anonymous> (/node_modules/tronweb/dist/TronWeb.node.js:1:697142) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.12.0

I don't know if I'm doing anything wrong or is there anything that is really not right!

start940315 commented 5 months ago

TronWeb v5.x is recommended to be used with bundlers such as webpack and vite. If you want to use directly, you can install the beta version:

npm install tronweb@beta
sinawic commented 5 months ago

hey, thanks for the reply

now the problem seems to have been solved but i get the following error:

index.js:22
  const wallet = tronweb.createRandom()
                         ^

TypeError: tronweb.createRandom is not a function
    at init (index.js:22:26)
    at Timeout._onTimeout (index.js:42:3)
    at listOnTimeout (node:internal/timers:573:17)
    at process.processTimers (node:internal/timers:514:7)

Node.js v20.12.0
raymondmeg commented 5 months ago

I had same problem...

raymondmeg commented 5 months ago

Temporary solution is :

  1. using Beta version
  2. create an instance like following const tronWebInst = new TronWeb.TronWeb({ 'fullHost': 'https://api.trongrid.io', 'headers': { "TRON-PRO-API-KEY": APIKey }, });

Yes, it looks ugly.

sinawic commented 5 months ago

I get the error exactly the time when i require/import tronweb the workaround I'm now able to run my project is to install version 4 of tronweb. "tronweb": "^4.4.0" to be exact

start940315 commented 5 months ago

Temporary solution is :

  1. using Beta version
  2. create an instance like following const tronWebInst = new TronWeb.TronWeb({ 'fullHost': 'https://api.trongrid.io', 'headers': { "TRON-PRO-API-KEY": APIKey }, });

Yes, it looks ugly.

You can use named imports like below:

import { TronWeb, utils as TronWebUtils, Trx, TransactionBuilder, Contract, Event, Plugin } from 'tronweb';
raymondmeg commented 5 months ago

Temporary solution is :

  1. using Beta version
  2. create an instance like following const tronWebInst = new TronWeb.TronWeb({ 'fullHost': 'https://api.trongrid.io', 'headers': { "TRON-PRO-API-KEY": APIKey }, });

Yes, it looks ugly.

You can use named imports like below:

import { TronWeb, utils as TronWebUtils, Trx, TransactionBuilder, Contract, Event, Plugin } from 'tronweb';

Ok , I see. you should update readme.md as:

ES Modules (ESM) import * CommonJS require()

personally , i never like ES6 ..since it's good to read the code ,but very very suck on debug.. Young developers don't care about the debug & error handler.. because their code "never had error", if program had "unexpected results", it must be the fault of customer.

sinawic commented 5 months ago

ES Modules (ESM) import * CommonJS require()

@raymondmeg

problem is require(***) does not work in latest version (5.x)

start940315 commented 5 months ago

ES Modules (ESM) import * CommonJS require()

@raymondmeg

problem is require(***) does not work in latest version (5.x)

Have you solved your problem? If not, please provide a demo to reproduce it so I can tell the problem.

sinawic commented 5 months ago

I get the error exactly the time when i require/import tronweb the workaround I'm now able to run my project is to install version 4 of tronweb. "tronweb": "^4.4.0" to be exact

turns out I'm not using any of the features of 5.x whenever I change the version to 5.x in my package.json file I get the error. no matther I change my project type to module or not (using require/import) I still get the error Error [ERR_REQUIRE_ESM]: require() of ES Module secp256k1/index.js from TronWeb.node.js @start940315

start940315 commented 5 months ago

I just figure out how this happened. You must have install the latest version of @noble/secp256k1. But you should install the @noble/secp256k1@1.7.1. So please try follow:

npm install @noble/secp256k1@1.7.1
sinawic commented 5 months ago

my problem is solved

npm install @noble/secp256k1@1.7.1

this was the key!

man thanks!