Telegram 机器人无法启动

问题描述 投票:0回答:1

我正在用 Node.js 编写我的第一个机器人,创建了一个空项目。 我在控制台中输入 (npm init -y) 并创建了 package.json 文件 我通过控制台添加的位置 (npm i [email protected]) 和 (npm i nodemon) 使用 Node.js v16.10.0

{
  "name": "second_project_bot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nodemon": "^3.0.3",
    "telegraf": "3.39"
  }
}

然后我创建了一个js文件并在那里编写了一段简单的代码

const { Telegraf } = require('telegraf')
const bot = new Telegraf('my token')

bot.command('start', (msg) => {
    console.log(msg);
    
})
bot.launch()

我尝试通过控制台中的命令(npm start)运行它,但收到此错误 enter image description here

我尝试安装另一个版本的 Node.js (20.11.0) 我认为这是问题所在,但安装新版本没有帮助

javascript node.js telegram-bot
1个回答
0
投票

index.js 文件应与 package.json 文件位于同一目录中。我怀疑你的index.js在src文件夹中:)

© www.soinside.com 2019 - 2024. All rights reserved.