dirigeants / klasa

A class remix of the Komada Bot Framework
https://klasa.js.org
MIT License
201 stars 87 forks source link

Module '"discord.js"' has no exported member 'ClientApplication'. #810

Closed enbermudas closed 5 years ago

enbermudas commented 5 years ago

Describe the issue

I just got the next error while initializing my discord bot:

Module '"discord.js"' has no exported member 'ClientApplication'.

Code or steps to reproduce

index.ts file

import { Client, KlasaClientConfig } from 'klasa';
import { config, token } from './config';

class CustomClient extends Client {
  constructor(options: KlasaClientConfig) {
    super(options);
  }
}

new CustomClient(config).login(token);

package.json script runned: "start"

"scripts": {
    "build": "yarn tsc",
    "lint": "tslint -c tslint.json 'src/**/*.ts'",
    "lint:fix": "yarn lint --fix",
    "start": "yarn build && node ./build/index.js",
    "tsc": "tsc"
}

Expected and actual behavior

Run just fine?

Further details

kyranet commented 5 years ago

It's exported, and nobody else gets this error:

https://github.com/discordjs/discord.js/blob/ea9e1441905e67e2627a6f5e80a8d989d01376c5/typings/index.d.ts#L248-L266

Some (+10) production bots are written in Klasa and use the latest commits, and they build fine.

My guess is that yarn corrupted your install, I suggest you to clean your yarn cache (yarn cache clean), nuke the node_modules folder (rm -r -Force node_modules in PowerShell, rm -rf node_modules in Linux), the yarn.lock, and re-install.

enbermudas commented 5 years ago

I did so by:

  1. running yarn cache clean
  2. deleting both node_modules and yarn.lock
  3. re-install my dependencies

The problem persist, any other idea?

MrJacz commented 5 years ago

@enbermudas klasa does not support discord.js v11. Klasa v0.4.0 aka stable doesn't actually work.

You will need to switch to klasa#master v1.0.0-alpha or settings branch and discord.js master aka v12-dev.

Discord.js

# Npm
npm i discordjs/discord.js
# Yarn
yarn add discordjs/discord.js

Klasa

# NPM
npm i dirigeants/klasa
# Yarn
yarn add dirigeants/klasa

If you want to use the v1.0.0-alpha or settings branch apply #branchname after dirigeants/klasa

enbermudas commented 5 years ago

I installed those two dependencies and there are 58 errors now within the typings.

kyranet commented 5 years ago

Has this been fixed yet? There was a discussion in the support server that led to a fix.

The culprit was the lack of "types": ["node", "discord.js", "klasa"] in the tsconfig.json's compilerOptions, and the error is pretty understandable as:

TSC (TypeScript Compiler) reads the main file, detects klasa is being imported, a module that defines both klasa itself (it's own definitions) and discord.js (augmented definitions), but since the real discord.js module has not been loaded, TSC takes the augmentations as the type source, which is not valid, causing this problem.

With the types being defined, TSC will load Node.js's types (which defines NodeJS.Timeout, Buffer, and others, which both discord.js and klasa rely on), then it will load discord.js, which defines ClientApplication and many other classes klasa imports, and finally, klasa itself.

That way when you load your project and TSC finds the import { Client } from 'klasa';, all the types are pre-loaded, and everything is available and valid.

bdistin commented 5 years ago

it was also because they were trying to compile to es4 or es5 code, instead of es6/esnext

sustained commented 4 years ago

I am having this issue using both master and settings branches even though I reordered the types as per the above message.

The issue ultimately comes from here:

// node_modules/discord.js/typings/index.d.ts
declare module 'discord.js' {
  import BaseCollection from '@discordjs/collection'; // <--
  // *snip*
}

My tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "outDir": "./dist",
    "baseUrl": ".",
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "types": ["node", "discord.js", "klasa"]
  },
  "include": ["./**/*", "./data/**/*"],
  "exclude": ["**/node_modules"]
}

Repro: https://github.com/sustained/i-hate-typescript