gpt4 book ai didi

javascript - 引用错误: Cannot access 'Player' before initialization

转载 作者:行者123 更新时间:2023-11-28 03:11:02 25 4
gpt4 key购买 nike

所以我一直在 Nodejs 上使用 ES6 风格的语法和 ESM 模块加载器进行导入/导出。一切都很好,直到我开始收到与导入有关的错误。

错误消息如下:

joseph@InsaneMachine:~/placeholder2/main-server$ npm start

> main-server@1.0.0 start /home/joseph/placeholder2/main-server
> nodemon --experimental-modules src/index.mjs

[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node --experimental-modules src/index.mjs`
(node:16942) ExperimentalWarning: The ESM module loader is experimental.
file:///home/joseph/placeholder2/main-server/src/games/game-player.mjs:3
export default class GamePlayer extends Player
^

ReferenceError: Cannot access 'Player' before initialization
at file:///home/joseph/placeholder2/main-server/src/games/game-player.mjs:3:41
at ModuleJob.run (internal/modules/esm/module_job.js:109:37)
at async Loader.import (internal/modules/esm/loader.js:132:24)
[nodemon] app crashed - waiting for file changes before starting...

以下是 Player 文件(基类):

import PasswordHash from 'password-hash';

import GamesService from '../games/games.service.mjs';

import PlayersService from './players.service.mjs';

import QueueingService from '../queueing/queueing.service.mjs';

export default class Player
{
constructor(object)
{
Object.assign(this, JSON.parse(JSON.stringify(object)));
}

get id()
{
return this._id.toString();
}

equals(other)
{
if(other.id != null)
return other.id == this.id;
return false;
}

checkPassword(password)
{
return PasswordHash.verify(password, this.password);
}

online()
{
return PlayersService.consumer.isPlayerOnline(this);
}

inQueue()
{
return QueueingService.queued(this);
}

inGame()
{
return GamesService.getActiveGameByPlayer(this) != null;
}

reduce()
{
return {
id: this.id,
username: this.username,
email: this.email,
admin: this.admin,
online: this.online(),
in_queue: this.inQueue(),
in_game: this.inGame(),
};
}

static hashPassword(password)
{
return PasswordHash.generate(password);
}

static schema = {
username: String,
password: String,
email: String,
email_confirmed: Boolean,
admin: Boolean,
}
}

和游戏玩家(子类):

import Player from '../players/player.mjs';

export default class GamePlayer extends Player
{
constructor(player, token)
{
super(player);
this.token = token;
}
}

以及项目的层次结构:

src/
-- games/
-- -- game-player.mjs
-- -- ...
players/
-- -- player.mjs
-- -- ...
-- ...

除非是其他原因,否则我该如何解决此导入问题?

编辑:据我所知,我没有使用 Babel,我正在使用 Node.js 提供的 --external-modules。不确定这是如何工作的。

最佳答案

我去了 Node.JS 论坛并询问可能是什么问题。根本不是 babel 问题,只是循环依赖。例如:

// A.js
import B from './B.js'
export default class A{}
// B.js
import A from './A.js'
export default class B extends A{}

抱歉,没有足够的信息来解决这个问题。我在 node.js github 上得到了很多帮助,有人在 github 上查看了我的项目,最终找到了一个两个模块相互指向的实例。

关于javascript - 引用错误: Cannot access 'Player' before initialization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60122727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com