从@react-three/drei导入Html时报DataUtils未从three导出错误如何解决
问题原因
该报错由three、@react-three/drei、three-stdlib三个依赖的版本不匹配导致:
- 你可以正常导入的无官方前缀的
drei是早期独立发布的旧版本,内部绑定的依赖已经做过适配,不会出现导出缺失问题;而@react-three/drei是迁移到React Three官方组织下的新版本,对配套的three版本有严格要求。three@0.150及以上版本移除了原有的DataUtils导出,如果你当前项目的three版本不符合@react-three/drei的要求,或是@react-three/drei依赖的three-stdlib版本不兼容,就会触发该报错。 - 你查阅的官方文档中关于web导出的说明,是针对React Native等多端适配场景的规则,和本次版本不匹配的问题无关。
解决方案
- 方案1:升级所有相关依赖到适配的最新版本
执行以下命令重装依赖即可:
重装完成后使用npm uninstall three drei @react-three/drei three-stdlib npm install three @react-three/drei three-stdlibimport { Html } from '@react-three/drei'即可正常导入使用。 - 方案2:项目不便升级
three版本时可继续使用旧版
保留当前import { Html } from 'drei'的导入方式即可,不要混用drei和@react-three/drei两个包避免出现额外的依赖冲突。 - 临时兼容方案:不想调整依赖版本时可手动指定导入web分支
写法如下:import { Html } from '@react-three/drei/web'
内容的提问来源于stack exchange,提问作者JustAsking
相关产品推荐
相关产品推荐

