Skip to content

【V5】引入G6-5.x报错TypeError: Cannot read properties of undefined (reading 'Graph') #6504

Answered by TZZack
TZZack asked this question in Q&A
Discussion options

You must be logged in to vote

感谢回复,非G6问题,我忘记回来补充了,是我这边的问题,排查过程和结果如下:
1、最终找到控制台实际报错的地方是dagre这个依赖里面的var Graph = require("./graphlib").Graph这行代码报的错,也就是require了个undefined进来
2、graphlib.js文件里面就是把graphlib这个库引入进来而已(这里有try catch),然后就接着往graphlib这个库排查
3、最终发现是graphlib里面的dijkstra.js这个文件的var DEFAULT_WEIGHT_FUNC = _.constant(1)报错了,因为这里的lodash函数编译处理后多了一层default,所以_.constant.default(1)才会正常
4、那为什么多了一层default呢,看着是lodash有问题就想起这个项目有个webpack插件把所有的lodash的import路径都改为lodash-es,graphlib里面的lodash是这样引入的var lodash = { ..., constant: require("lodash/constant") },也就是变为了lodash-es/constant,然后graphlib是cjs,就可能是webpack处理的时候没处理好吧(这个目前没时间深究,后续有时间继续排查下)
(目前是把这个插件改了下,不处理cjs库的lodash了)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TZZack
Comment options

Answer selected by TZZack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants