【问题标题】:How to resolve circular dependency in compilation of node modules?如何解决节点模块编译中的循环依赖?
【发布时间】:2021-06-20 15:12:14
【问题描述】:

我正在使用 yarn 工作区并且有一个项目 (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.js reactjs yarnpkg circular-dependency yarn-workspaces


    【解决方案1】:

    解决了。这个问题实际上很基本。我意识到我是从@visx/axis 导入的,而我的package.json 中没有明确包含它。 ? 显式添加后,就可以了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 2011-05-29
      • 2021-09-17
      • 2012-03-15
      • 2016-09-21
      • 2012-11-16
      相关资源
      最近更新 更多