tronprotocol / tronweb

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

node.js, get error when want to use #492

Closed fastikprog closed 4 months ago

fastikprog commented 4 months ago
const TronWebLib = require('tronweb');
const configurations = require('./config')

class TronService {
    constructor() {
        this.tronWeb = new TronWebLib({
            fullHost: 'https://api.trongrid.io',
            headers: { "TRON-PRO-API-KEY": configurations.tronGridKey },
            privateKey: configurations.tronWalletSecretKey
        });
    }

    async isAddress(address) {
        return await this.tronWeb.isAddress(address);
    }

    async getAddressBalance(address) {
        const balance = await this.tronWeb.trx.getBalance(address);
        return balance / 1000000;
    }

    async sendTRX(receiver, amount) {
        try {
            const trx = await this.tronWeb.trx.sendTransaction(receiver, amount * 1000000, configurations.tronWalletSecretKey);
            return trx.transaction.txID;
        } catch (e) {
            console.error(e);
        }
    }
}

module.exports = { TronService };

node:internal/modules/cjs/loader:1423 throw err; ^ Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\User\Desktop\Projects\Exchane bot\node_modules\@noble\secp256k1\index.js from C:\Users\User\Desktop\Projects\Exchane bot\node_modules\tronweb\dist\TronWeb.node.js not supported. Instead change the require of index.js in C:\Users\User\Desktop\Projects\Exchane bot\node_modules\tronweb\dist\TronWeb.node.js to a dynamic import() which is available in all CommonJS modules.
at C:\Users\User\Desktop\Projects\Exchane bot\node_modules\tronweb\dist\TronWeb.node.js:1:296544 at C:\Users\User\Desktop\Projects\Exchane bot\node_modules\tronweb\dist\TronWeb.node.js:1:697113 at Object. (C:\Users\User\Desktop\Projects\Exchane bot\node_modules\tronweb\dist\TronWeb.node.js:1:697142) { code: 'ERR_REQUIRE_ESM' } Node.js v21.7.0