课程要求

时间:2019-01-30 15:37:05

标签: javascript typescript require

我在编写用于分别加载模块的脚本时遇到了问题,该脚本允许修改和重写代码。

文件结构:

base.ts
(other unimportant files)
-- extensions/app/app.ts
-- extensions/app/global/events/ready.ts

我试图以几种方式更改此脚本,但似乎无法弄清楚我做错了什么。可能是我傻了,但你知道。 xD

class globalEventsLoader {
    eventsDirectory = "./extensions/app/global/events";
    core: any;
    constructor(core: any) {
        this.core = core;
    }
    loadModules = () => {
        fs.readdir(this.eventsDirectory, (err, files) => {
            console.log(`Global Events Files are: ${files}`)
            if (err) {throw err;}
            files.forEach(file => {
                this.initializeEvent(file)
            })

        })
    }
    initializeEvent = (file: any) => {
      console.log(`Global Events File is: ${file}`)
      let event = require(`${this.eventsDirectory}/${file}`);
      let eventName = file.split(".")[0];
      client.on(eventName, event.bind(null, this.core, client));
      console.log(`Attempting to load global event "${eventName}" (${this.eventsDirectory}/${file}) `);
    }
}

预期结果是将文件绑定到事件的结果。实际结果是:

Global Events Files are: ready.js
Global Events File is: ready.js
internal/modules/cjs/loader.js:582
    throw err;
    ^

Error: Cannot find module './extensions/app/global/events/ready.js'

非常感谢

0 个答案:

没有答案