discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

@discordjs/rest: Could not find a declaration file for module 'node-fetch' #6922

Closed r1zyn closed 2 years ago

r1zyn commented 2 years ago

Issue description

Whenever I try to compile to JavaScript from TypeScript, the compiler throws the errors showing at the bottom. I tried installed @types/node-fetch and url again, but the problem still persisted. Note: the src folder contains the TypeScript code and the dist folder contains the compiled JavaScript code.

Note: The node_modules folder is not included in the repository.

Reproduction You can find the code in my GitHub repository here. Make sure you have TypeScript installed. Step 1: Create an .env file in the root directory. The variables are TOKEN (your bot's token) Step 2: Run npm run dev in the terminal (this script is declared in the scripts property in package.json)

TypeScript should throw the following errors: image

Code sample

// First error in node_modules/@discordjs/rest/dist/lib/handlers/IHandler.d.ts
import type { RequestInit } from 'node-fetch';
import type { RouteData } from '../RequestManager';
export interface IHandler {
    queueRequest(routeID: RouteData, url: string, options: RequestInit): Promise<unknown>;
}
//# sourceMappingURL=IHandler.d.ts.map

// Second error in node_modules/@discordjs/rest/dist/lib/RequestManager.d.ts
/// <reference types="node" />
import Collection from '@discordjs/collection';
import { EventEmitter } from 'events';
import type { IHandler } from './handlers/IHandler';
import type { RESTOptions } from './REST';
/**
 * Represents an attachment to be added to the request
 */
export interface RawAttachment {
    fileName: string;
    rawBuffer: Buffer;
}
/**
 * Represents possible data to be given to an endpoint
 */
export interface RequestData {
    /**
     * Files to be attached to this request
     */
    attachments?: RawAttachment[];
    /**
     * If this request needs the `Authorization` header
     * @default true
     */
    auth?: boolean;
    /**
     * The authorization prefix to use for this request, useful if you use this with bearer tokens
     * @default 'Bot'
     */
    authPrefix?: 'Bot' | 'Bearer';
    /**
     * The body to send to this request
     */
    body?: unknown;
    /**
     * Additional headers to add to this request
     */
    headers?: Record<string, string>;
    /**
     * Query string parameters to append to the called endpoint
     */
    query?: URLSearchParams;
    /**
     * Reason to show in the audit logs
     */
    reason?: string;
    /**
     * If this request should be versioned
     * @default true
     */
    versioned?: boolean;
}
/**
 * Possible headers for an API call
 */
export interface RequestHeaders {
    Authorization?: string;
    'User-Agent': string;
    'X-Audit-Log-Reason'?: string;
}
/**
 * Possible API methods to be used when doing requests
 */
export declare const enum RequestMethod {
    Delete = "delete",
    Get = "get",
    Patch = "patch",
    Post = "post",
    Put = "put"
}
export declare type RouteLike = `/${string}`;
/**
 * Internal request options
 *
 * @internal
 */
export interface InternalRequest extends RequestData {
    method: RequestMethod;
    fullRoute: RouteLike;
}
/**
 * Parsed route data for an endpoint
 *
 * @internal
 */
export interface RouteData {
    majorParameter: string;
    bucketRoute: string;
    original: string;
}
/**
 * Represents the class that manages handlers for endpoints
 */
export declare class RequestManager extends EventEmitter {
    #private;
    /**
     * A timeout promise that is set when we hit the global rate limit
     * @default null
     */
    globalTimeout: Promise<void> | null;
    /**
     * API bucket hashes that are cached from provided routes
     */
    readonly hashes: Collection<string, string>;
    /**
     * Request handlers created from the bucket hash and the major parameters
     */
    readonly handlers: Collection<string, IHandler>;
    readonly options: RESTOptions;
    constructor(options: Partial<RESTOptions>);
    /**
     * Sets the authorization token that should be used for requests
     * @param token The authorization token to use
     */
    setToken(token: string): this;
    /**
     * Queues a request to be sent
     * @param request All the information needed to make a request
     * @returns The response from the api request
     */
    queueRequest(request: InternalRequest): Promise<unknown>;
    /**
     * Creates a new rate limit handler from a hash, based on the hash and the major parameter
     * @param hash The hash for the route
     * @param majorParameter The major parameter for this handler
     * @private
     */
    private createHandler;
    /**
     * Formats the request data to a usable format for fetch
     * @param request The request data
     */
    private resolveRequest;
    /**
     * Generates route data for an endpoint:method
     * @param endpoint The raw endpoint to generalize
     * @param method The HTTP method this endpoint is called without
     * @private
     */
    private static generateRouteData;
}
//# sourceMappingURL=RequestManager.d.ts.map

discord.js version

13.2.0

Node.js version

16.5.0

Operating system

Windows 10 Home

Priority this issue should have

Medium (should be fixed soon)

Which partials do you have configured?

USER, CHANNEL, GUILD_MEMBER, MESSAGE, REACTION

Which gateway intents are you subscribing to?

GUILDS, GUILD_MEMBERS, GUILD_BANS, GUILD_EMOJIS_AND_STICKERS, GUILD_INTEGRATIONS, GUILD_WEBHOOKS, GUILD_INVITES, GUILD_VOICE_STATES, GUILD_PRESENCES, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGE_TYPING, DIRECT_MESSAGES, DIRECT_MESSAGE_REACTIONS, DIRECT_MESSAGE_TYPING

I have tested this issue on a development release

761a901

megatank58 commented 2 years ago

Your node-fetch version is 2.x.x but your @types/node-fetch is 3.x.x, i recommend installing @types/node-fetch@cjs for node-fetch 2.x.x

Also please ask in the discord server for general questions

r1zyn commented 2 years ago

Alright, I’ll try it out. I tried asking in the server but no-one knew so I ended up opening an issue.

r1zyn commented 2 years ago

Your node-fetch version is 2.x.x but your @types/node-fetch is 3.x.x, i recommend installing @types/node-fetch@cjs for node-fetch 2.x.x

Also please ask in the discord server for general questions

@types/node-fetch@cjs doesn't exist in the NPM registry.

megatank58 commented 2 years ago

Your node-fetch version is 2.x.x but your @types/node-fetch is 3.x.x, i recommend installing @types/node-fetch@cjs for node-fetch 2.x.x

Also please ask in the discord server for general questions

@types/node-fetch@cjs doesn't exist in the NPM registry.

Oh my bad, it would be @types/node-fetch@2

r1zyn commented 2 years ago

Yeah, I found this here. I installed an older version and it worked fine. https://github.com/node-fetch/node-fetch/issues?q=Could+not+find+a+declaration+file+for+module+%27node-fetch%27