UnhandledPromiseRejectionWarning: ReferenceError: request is not defined || sharp

UnhandledPromiseRejectionWarning: ReferenceError: request is not defined || sharp

我正在使用 sharp,想制作一个图像编辑 discord 机器人。我当然需要为此使用 URL,所以我尝试了它并在 GitHub 页面中看到了一个请求函数。但是,它似乎对我不起作用。错误在标题中,这里是代码:


const Discord = require("discord.js")
const sharp = require("sharp")
const client = new Discord.Client()
const url = "https://934185.smushcdn.com/2425611/wp-content/uploads/2021/05/shiba-inu-2.jpeg"

async function startup() {
    request({ url, encoding: null }, function(error, response, body) {
        if (!error) {
            sharp(body).rotate().resize(300, 300).toBuffer().then(function(data) {
                console.log(data)
            })
        }
    })
}


startup()


client.on("ready", () => {
    console.log("Ready!")
})

client.login(process.env.token)

您需要将请求导入您的程序。 要安装请求模块,只需在您的控制台中输入:

npm i request

然后在您的代码中这样要求它:

const request = require('request');

更好的是,request 已被弃用,因此您可以使用 Axios 之类的东西。

https://www.npmjs.com/package/axios