skip to Main Content

Why I have This error:
TypeError: node_telegram_bot_api_1.default is not a constructor

This is My Code in TypeScript:

import * as dotenv from 'dotenv';
dotenv.config({ path: __dirname + '/.env'})
console.log('Hello TypeScript')
import TelegramBot from 'node-telegram-bot-api';    
const bot = new TelegramBot(process.env.BOT_TOKEN, {polling: true});

And This is My Output Code after Compile:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv = require("dotenv");
dotenv.config({ path: __dirname + '/.env' });
console.log('Hello TypeScript');
const node_telegram_bot_api_1 = require("node-telegram-bot-api");
const bot = new node_telegram_bot_api_1.default(process.env.BOT_TOKEN, { polling: true });

Photo

2

Answers


  1. It seems that the import is incorrectly done. The documentation of node-telegram-bot-api says that the import needs to be done as follows:

    const TelegramBot = require('node-telegram-bot-api');
    

    This means that the whole module is being imported, which translates to ES6 import as follows:

    import * as TelegramBot from 'node-telegram-bot-api';
    

    For different syntax and semantics of import please refer this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

    Login or Signup to reply.
  2. I have the same problem and I solved it by replacing

        const Telegraf = require('telegraf')
    

    with

        const { Telegraf } = require('telegraf')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search