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 });
2
Answers
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:This means that the whole module is being imported, which translates to ES6 import as follows:
For different syntax and semantics of import please refer this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
I have the same problem and I solved it by replacing
with