Help me solve the problem. I installed type:module in package.json but the error did not go away. I’ve already tried all the advice from the Internet but nothing helps, I’ve already reinstalled everything, but the error remains
node:internal/process/esm_loader:40
internalBinding(‘errors’).triggerUncaughtException(
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/t-rex/server-t-rex/node_modules/@noble/secp256k1/index.js from /home/t-rex/server-t-rex/node_modules/tronweb/dist/TronWeb.node.js not supported.
Instead change the require of index.js in /home/t-rex/server-t-rex/node_modules/tronweb/dist/TronWeb.node.js to a dynamic import() which is available in all CommonJS modules.
at /home/t-rex/server-t-rex/node_modules/tronweb/dist/TronWeb.node.js:1:296544
at /home/t-rex/server-t-rex/node_modules/tronweb/dist/TronWeb.node.js:1:697113
at Object. (/home/t-rex/server-t-rex/node_modules/tronweb/dist/TronWeb.node.js:1:697142) {
code: ‘ERR_REQUIRE_ESM’
}
import { Telegraf } from 'telegraf';
import crypto from 'crypto';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import TronWeb from 'tronweb'
dotenv.config();
const { TELEGRAM_BOT_TOKEN, MONGO_URI, BOT_SECRET } = process.env;
const tronWeb = new TronWeb({
fullHost: 'https://api.trongrid.io',
});
const bot = new Telegraf(TELEGRAM_BOT_TOKEN);
mongoose.connect(MONGO_URI, {});
const userSchema = new mongoose.Schema({
id: Number,
username: String,
first_name: String,
hash: String,
user_refid: Number,
status_ref: { type: Number, default: 0 }
});
const walletSchema = new mongoose.Schema({
telegram_id: {
type: Number,
required: true,
ref: 'User',
},
address_base58: {
type: String,
required: true,
},
publicKey: {
type: String,
required: true,
},
privateKey: {
type: String,
required: true,
}
});
const User = mongoose.model('User', userSchema);
const Wallet = mongoose.model('Wallet', walletSchema);
const computeHash = (data, secret) => {
const dataCheckString = Object.keys(data).sort().map(key => `${key}=${data[key]}`).join('n');
return crypto.createHmac('sha256', secret).update(dataCheckString).digest('hex');
};
bot.start(async (ctx) => {
const { id, username, first_name } = ctx.from;
const user = {
id,
username,
first_name,
hash: computeHash({ id }, BOT_SECRET),
user_refid: ctx.startPayload ? parseInt(ctx.startPayload, 10) : undefined
};
try {
const existingUser = await User.findOne({ hash: user.hash }).exec();
if (!existingUser) {
const savedUser = await User.findOneAndUpdate({ id: user.id }, user, { upsert: true, new: true }).exec();
const account = await tronWeb.createAccount();
const newWallet = {
telegram_id: savedUser.id,
address_base58: account.address.base58,
publicKey: account.publicKey,
privateKey: account.privateKey
};
await Wallet.create(newWallet);
console.log('User and wallet created:', savedUser, newWallet);
}
} catch (err) {
console.error('Error saving user to MongoDB:', err);
}
});
bot.launch();
console.log('Bot is running...');
Pasha B is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.