I am trying to import a class from one file to another, but when I do import it, it returns undefined
for a couple hundred milliseconds, then I get the value that I am looking for:
// commands.js
export class Command {
// ...
}
// cmds/Help.js
import {Command} from './../commands.js'
console.log(Command) // => undefined
setTimeout(() => console.log(Command), 1000) // => [Function: Command]
So, of course, when I try to extend the imported class, I get errors saying that undefined in not a constructor. The only solution that I can see is to wrap everything inside the cmds/
directory with a setTimeout()
, but that would not be efficient. I have messed around with using async
/await
functions and promises, but neither of those solved the issue.
Edit 1
I'm trying to post as much of the requested information as I can, but I am away from my computer so I'm posting all that I can for right now.
The code is running on Node 9.5, and is transpiled by Babel env
set to "node": "current"
.
As for the code, here is all I can give you right now:
// Directory structure
[src]
|- cmds/
| |- Help.js
| |- Gh.js
| \...
|- commands.js
|- data.js
|- send-msg.js
\... several more js and json files
// commands.js
import {readData, updateUserData} from './data.js'
import {sendMsg} from './send-msg.js'
//import * as cmdList from './cmds/*' // using babel-plugin-wildcard
export class Command {
constructor (msg) {
this.id = msg.author.id
this.msg = msg
}
action () {}
get data () {
return readData().user[this.id]
}
updateUserData (key, val) {
updateUserData(this.id, key, val)
}
sendMsg (data) {
sendMsg(this.msg, data)
}
}
// there is an exported function, but I do not have the contents atm
// cmds/Gh.js
// There are several similar files in cmds/, including this one and the one
// mentioned in the question, Help.js; there are more. They all have
// the same issue, though.
import {Command} from '../commands'
export class Gh extends Command {
constructor (msg) {
super(msg)
this.desc = 'Returns GitHub repository link and exits'
}
action () {
this.sendMsg('GitHub link: https://github.com/owm111/knife-wife')
}
}
BTW: the GitHub repository above does not have this code in it yet, but everything that is in it is functioning (i.e. not experiencing this issue).