Skip to content

"Module not found" when extending a class otherwise same code without extending no error #51260

@AlienDwarf

Description

@AlienDwarf

Bug Report

Every time when I try to extend the abtract class Command and I try to compile I get the error: Error: Cannot find module src/classes/commandBase

🔎 Search Terms

  • Module not found
  • class module not found

🕗 Version & Regression Information

  • v4.8.4

💻 Code

My project has the following structure:

   ├────tsconfig.json
   └────src
        ├───classes
        │   └───commandBase.ts
        ├───commands
        │   └───ping.ts
        ├...

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "CommonJS",
    "rootDir": "./src/",
    "outDir": "./dist/",
    "strict": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "forceConsistentCasingInFileNames": true,
    "removeComments": true,
    "typeRoots": ["node_modules/@types"],
    "sourceMap": true,
    "baseUrl": "./"
  },
  "include": ["./**/**/*.ts"],
  "exclude": ["node_modules", "dist"]
}

commandBase.ts

abstract class Command {
  name: string;
  abstract execute(client: Client, message: Message, args: string[]): void;

  constructor(name: string) {
    this.name = name;
  }
}
export default Command;

ping.ts

import Command from "src/classes/commandBase";

class Ping extends Command {
  constructor() {
    super("ping");
  }
  async execute(
    client: Client<boolean>,
    message: Message<boolean>,
    args: string[]
  ): Promise<void> {
    const m = await message.channel.send("Ping");
    m.edit(
      `Pong! Latency is ${
        m.createdTimestamp - message.createdTimestamp
      }ms. Discord Chat API Latency is ${Math.round(client.ws.ping)}ms`
    );
  }
}

export default Ping;

🙁 Actual behavior

Error: Cannot find module 'src/classes/commandBase'.

However if you change import Command from "src/classes/commandBase"; to import Command from "../classes/commandBase"; everything is working fine when importing.

BUT the weird behavior starts now:
If you create a new file which import Command from 'src/classes/commandBase and exports a variable everything is working as intended. The error only appears when you extend a class.

file foo.ts

// This is working 
import Command from "src/classes/commandBase";

const foo: Command = {
  name: "",
  execute: function (
    client: Client,
    message: Message,
    args: string[]
  ): void {
    throw new Error("Function not implemented.");
  },
};
export default foo;

🙂 Expected behavior

Since the module exists it should be able to import.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions