TypeError: Class extends value undefined is not a constructor or null

匿名 (未验证) 提交于 2019-12-03 01:06:02

问题:

This problem has been causing me to lose my sanity for the last couple of days.

Here is my directory structure:

[src] |- cmds/ |  |- Gh.js |  \- Help.js |- commands.js |... 

I am trying to import a class exported by commands.js into Help.js and Gh.js (and any other files I might add in the future). However, I keep getting an error:

class Gh extends _commands.Command {                            ^ TypeError: Class extends value undefined is not a constructor or null 

All of the files are being transpiled using Babel, with env set to "node": "current" and using the wildcard package. I have tried to set it for "browser" to see if it was an issue of it being too "advanced", but I got a different error about super functions (or something), which I assume is the same issue.

Here is the class being exported from commands.js:

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)   }  } 

...and here is cmds/Gh.js, one of the files that I am trying to import Command into:

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')   } } 

I tried putting Command into both of the cmds/, and they worked perfectly. However, when moving it back into commands.js, it broke again. I tried changing the path it is importing from from ../commands to ./../commands, ../commands.js, ./../commands.js; none worked. I moving commands.js into cmds/, still broke. I tried to console.log(Command) in both of the cmds/, but they both returned undefined.

All of this makes it look like is a problem with importing, but I cannot figure out what for the life of me. Please help.

回答1:

This is just a simple fix for node.js. Remove export from your class and at the bottom of your file put this in it.

module.exports.Command; 

Now if you want to use the command class anywhere you just need to put this in each file where you would like to use it.

var { Command } = require('Command.js'); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!