TypeError when using React: Cannot read property 'firstChild' of undefined

后端 未结 6 1191
误落风尘
误落风尘 2020-12-29 03:22

Sometimes, when using React libraries, such as react-router, I get this error:

Uncaught TypeError: Cannot read property \'firstChild\' of undefined

6条回答
  •  执笔经年
    2020-12-29 04:13

    In case anyone has this issue having npm linked two modules depending on react, I found a solution...

    Let's say you have Parent depending on React, and Child depending on react. When you do:

    cd ../child npm link cd ../parent npm link child

    This causes this problem, because parent and child will each load their own instance of React.

    The way to fix this is as follows:

    cd parent cd node_modules/react npm link cd ../../../child npm link react

    This ensures your parent project supplies the react dependency, even when linked, which is how npm would resolve the dependency when you are unlinked.

提交回复
热议问题