Skip to content
Advertisement

Telegram Bot is not a constructor • TypeScript

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement