最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Telegram Bot is not a constructor • TypeScript - Stack Overflow

programmeradmin1浏览0评论

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 });

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 });

Share Improve this question edited Apr 15, 2017 at 4:52 Saeed Heidarizarei asked Apr 15, 2017 at 4:44 Saeed HeidarizareiSaeed Heidarizarei 8,91622 gold badges66 silver badges111 bronze badges 1
  • 3 Looks like the problem is with incorrect import. Have you tried this already import * as TelegramBot from 'node-telegram-bot-api';? – Sayan Pal Commented Apr 15, 2017 at 5:21
Add a comment  | 

2 Answers 2

Reset to default 10

I have the same problem and I solved it by replacing

    const Telegraf = require('telegraf')

with

    const { Telegraf } = require('telegraf')

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

发布评论

评论列表(0)

  1. 暂无评论