首页 > 解决方案 > 如何解决节点模块编译中的循环依赖?

问题描述

我正在使用纱线工作区并且有一个项目(proj1),它使用我编写的库(proj2),当我启动它时,它已经用 create-react-library 进行了初始化。我的库在某些组件中使用了 visx。一切都很好,直到我在 index.js 文件中添加了最后一行,导出了另一个使用 visx 构建的组件:

export { ConfigField } from './ConfigField';
export { ConfigPage } from './ConfigPage';
export { SaveButton } from './SaveButton';
export { PageLoadingIndicator } from './PageLoadingIndicator';
export { UsersPage } from './UsersPage';
export { AnalyticValue } from './AnalyticValue';
export { DonutChart } from './DonutChart';
export { SingleValueDonutChart } from './SingleValueDonutChart';
export { Alert } from './Alert';
export { FormDialog } from './FormDialog';
export { TimeSeriesChart } from './TimeSeriesChart';

不知何故,在添加此导出后,在构建 proj2 时会创建一个循环依赖项。我怀疑这与编译有 CJS 导入有关。

PS C:\usr\git\proj2> yarn
yarn install v1.22.5
warning package.json: No license field
[1/4] Resolving packages...
success Already up-to-date.
$ run-s build
yarn run v1.22.5
warning package.json: No license field
$ microbundle-crl --no-compress --format modern,cjs
Circular dependency: ..\node_modules\@visx\axis\esm\index.js -> ..\node_modules\@visx\axis\esm\axis\Axis.js -> ..\node_modules\@visx\axis\esm\axis\AxisRenderer.js -> ..\node_modules\@visx\axis\esm\index.js
Non-existent export 'TextProps' is imported from ..\node_modules\@visx\text\esm\types.js
Error: 'TextProps' is not exported by ..\node_modules\@visx\text\esm\types.js, imported by ..\node_modules\@visx\text\esm\Text.js

at C:\usr\git\proj1\node_modules\@visx\text\esm\Text.js:8:9

 6: import useText from './hooks/useText';
 7: import { TextProps } from './types';
 8: export { TextProps } from './types';
             ^
 9: var SVG_STYLE = {
10:   overflow: 'visible'

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "build" exited with 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

如何解决这种循环依赖?有我应该使用的插件吗?

标签: node.jsreactjsyarnpkgcircular-dependencyyarn-workspaces

解决方案


解决了。这个问题实际上很基本。我意识到我是从@visx/axis没有明确地在我的package.json. 明确添加后,它可以工作。


推荐阅读