tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
388 stars 259 forks source link

require() of ES Module #493

Closed rzadrzi closed 4 months ago

rzadrzi commented 4 months ago

Hi! This error occurs for both modules TronWeb and @noble/secp256k1

svein1010 commented 4 months ago

Hi, could you please give some more detail about the error like the version and the detailed error infomation. It's much better if you have a reproduce repo. Thanks a lot.

List525 commented 4 months ago

Hi, I have the same problem. This error occurs for both TronWeb and @noble/secp256k1 modules, has anyone solved this problem yet?

rzadrzi commented 4 months ago

1) When i add "type": "commonjs" in my package.json 1-1) use require() Error [ERR_REQUIRE_ESM]: require() of ES Module @./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. 1-2) use import() SyntaxError: Cannot use import statement outside a module 2) When i add "type": "commonjs" in my package.json 2-1) use require() ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension and 'package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. 2-1-1) so i use ".cjs" extension Error [ERR_REQUIRE_ESM]: require() of ES Module @./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. 2-2) use import() Error [ERR_REQUIRE_ESM]: require() of ES Module /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.

Node.js v21.2.0 npm v10.2.4

On Sat, Mar 16, 2024 at 12:05 PM List525 @.***> wrote:

Hi, I have the same problem. This error occurs for both TronWeb and @noble/secp256k1 modules, has anyone solved this problem yet?

— Reply to this email directly, view it on GitHub https://github.com/tronprotocol/tronweb/issues/493#issuecomment-2001909618, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIY4V3R765G7KOP34UFRKL3YYP73RAVCNFSM6AAAAABEXDSXEWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBRHEYDSNRRHA . You are receiving this because you authored the thread.Message ID: @.***>

-- Reza Darzi CEO elsa. http://www.elsaagency.com Digital Marketing Agency

svein1010 commented 4 months ago

Are you using TronWeb v6.0.0-beta.1 or v5 ? It's recommended to use a build tools like webpack or vite if you are using v5. You can also have a try with TronWeb v6 beta version. V6 exports multiple type files including ESM and Commonjs.

npm install tronweb@beta
rzadrzi commented 4 months ago

thanks it works with vite

On Sun, Mar 17, 2024 at 10:43 AM Svein @.***> wrote:

Are you using TronWeb v6.0.0-beta.1 or v5 ? It's recommended to use a build tools like webpack or vite if you are using v5. You can also have a try with TronWeb v6 beta version https://tronweb.network/docu/docs/6.0.0-beta.1/intro. V6 exports multiple type files including ESM and Commonjs.

npm install @.***

— Reply to this email directly, view it on GitHub https://github.com/tronprotocol/tronweb/issues/493#issuecomment-2002340018, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIY4V3QSLDS2P6NJLJDRIZDYYU7CFAVCNFSM6AAAAABEXDSXEWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAMBSGM2DAMBRHA . You are receiving this because you authored the thread.Message ID: @.***>

-- Reza Darzi CEO elsa. http://www.elsaagency.com Digital Marketing Agency

basharov commented 4 months ago

I had the same problem, and using beta version helped, thanks!