NodeJS中如何解释循环引用在循环中的具体实现?

更新于
2026-10-01 06:09:07
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计747个文字,预计阅读时间需要3分钟。

NodeJS中如何解释循环引用在循环中的具体实现?

最近用node排查问题时,发现半天都解决不了,最后发现是循环引用导致的问题。以下是详细记录:

场景复现:- 比较简单的场景,涉及四个文件:parent.ts、child.ts、child_2.ts、util.ts- 文件内容如下:

typescript// parent.tsimport { util } from './util';

function parentFunc() { util();}

export { parentFunc };

// child.tsimport { parentFunc } from './parent';

function childFunc() { parentFunc();}

export { childFunc };

// child_2.tsimport { childFunc } from './child';

function child_2Func() { childFunc();}

export { child_2Func };

// util.tsexport function util() { console.log('util function called');}

发现问题:- 当尝试运行child_2Func时,程序会抛出错误:Error: Cannot find module './child'

原因分析:- 由于循环引用,导致child.ts无法正确导入parent.ts,进而导致child_2.ts无法导入child.ts,最终导致util.ts无法被导入。

最近在用node的时候排查一个问题排查了半天,最终发现是循环引用导致的问题,故在此记录一下。

场景复现

出现问题场景比较简单,一共四个类:

  • parent.ts
  • child.ts
  • child_2.ts
  • util.ts

export abstract class Parent { abstract hello(): string; }

import {Parent} from "./parent"; export class Child extends Parent { hello(): string { return "child"; } }

import {Child} from "./child"; export class Util { static useChildInSameCase(): string { let child: Child; return child.hello(); } }

import {Parent} from "./parent"; export class Child_2 extends Parent { hello(): string { return "child_2"; } }

这个时候我们去构造一个Child类:

import {Child} from "./child"; console.log(new Child().func());

就会直接报错了:

class Child_2 extends parent_1.Parent {
^

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

#寻找原因

说的是这个父类是一个undefined,很明显就是没有初始化。

一开始我觉得很奇怪,明明在child_2这个文件里已经import了parent,为什么会是undefined呢?后来debug查了一下代码的堆栈,恍然大悟:

NodeJS中如何解释循环引用在循环中的具体实现?

入口文件->child.ts->parent.ts->util.ts->child_2.ts->parent.ts

很明显这里存在着一个循环引用,当我们在加载child_2.ts这个文件的时候,parent.ts还处在未加载完的状态。

我们可以去官网看一下node中是如何处理循环引用的 。

通过官网我们可以知道,对于这样的循环引用,在child_2.ts加载parent.ts的时候,会去缓存中寻找,而由于parent.ts还未加载完成,所以缓存中会是一个空对象了,官网中用的语句是an unfinished copy of the a.js 。

解决方案

知道原因之后,解决方案也就变得清晰了起来,一句话搞定,将parent.ts中的import语句放在后面:

export abstract class Parent { abstract hello(): string; func(): string { return Util.useChildInSameCase(); } } import {Util} from "./util";

这样在加载parent.ts的时候,就会先export对象,然后再import所需要的util.ts了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。

本文共计747个文字,预计阅读时间需要3分钟。

NodeJS中如何解释循环引用在循环中的具体实现?

最近用node排查问题时,发现半天都解决不了,最后发现是循环引用导致的问题。以下是详细记录:

场景复现:- 比较简单的场景,涉及四个文件:parent.ts、child.ts、child_2.ts、util.ts- 文件内容如下:

typescript// parent.tsimport { util } from './util';

function parentFunc() { util();}

export { parentFunc };

// child.tsimport { parentFunc } from './parent';

function childFunc() { parentFunc();}

export { childFunc };

// child_2.tsimport { childFunc } from './child';

function child_2Func() { childFunc();}

export { child_2Func };

// util.tsexport function util() { console.log('util function called');}

发现问题:- 当尝试运行child_2Func时,程序会抛出错误:Error: Cannot find module './child'

原因分析:- 由于循环引用,导致child.ts无法正确导入parent.ts,进而导致child_2.ts无法导入child.ts,最终导致util.ts无法被导入。

最近在用node的时候排查一个问题排查了半天,最终发现是循环引用导致的问题,故在此记录一下。

场景复现

出现问题场景比较简单,一共四个类:

  • parent.ts
  • child.ts
  • child_2.ts
  • util.ts

export abstract class Parent { abstract hello(): string; }

import {Parent} from "./parent"; export class Child extends Parent { hello(): string { return "child"; } }

import {Child} from "./child"; export class Util { static useChildInSameCase(): string { let child: Child; return child.hello(); } }

import {Parent} from "./parent"; export class Child_2 extends Parent { hello(): string { return "child_2"; } }

这个时候我们去构造一个Child类:

import {Child} from "./child"; console.log(new Child().func());

就会直接报错了:

class Child_2 extends parent_1.Parent {
^

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

#寻找原因

说的是这个父类是一个undefined,很明显就是没有初始化。

一开始我觉得很奇怪,明明在child_2这个文件里已经import了parent,为什么会是undefined呢?后来debug查了一下代码的堆栈,恍然大悟:

NodeJS中如何解释循环引用在循环中的具体实现?

入口文件->child.ts->parent.ts->util.ts->child_2.ts->parent.ts

很明显这里存在着一个循环引用,当我们在加载child_2.ts这个文件的时候,parent.ts还处在未加载完的状态。

我们可以去官网看一下node中是如何处理循环引用的 。

通过官网我们可以知道,对于这样的循环引用,在child_2.ts加载parent.ts的时候,会去缓存中寻找,而由于parent.ts还未加载完成,所以缓存中会是一个空对象了,官网中用的语句是an unfinished copy of the a.js 。

解决方案

知道原因之后,解决方案也就变得清晰了起来,一句话搞定,将parent.ts中的import语句放在后面:

export abstract class Parent { abstract hello(): string; func(): string { return Util.useChildInSameCase(); } } import {Util} from "./util";

这样在加载parent.ts的时候,就会先export对象,然后再import所需要的util.ts了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对易盾网络的支持。