Search code examples
reactjsreact-pdf

react-pdf 5.7.1 throws error message: Module not found: Can't resolve 'yoga-layout/sync'


I am using the module react-pdf with version 5.7.1.

After installing all the dependencies including @react-pdf with npm install the project did always compile successfully in the past.

Since yesterday the project does not compile anymore and the following error is thrown:

./node_modules/@react-pdf/layout/lib/index.es.js

Module not found: Can't resolve 'yoga-layout/sync' in '...\node_modules@react-pdf\layout\lib'

The version of react-pdf did not change.

I found that the Yoga import changed in the files node_modules@react-pdf\layout\lib\index.cjs.js and node_modules@react-pdf\layout\lib\index.es.js. This seems because of an current update in the submodule @react-pdf/layout.

The new import is the following:

import Yoga from 'yoga-layout/sync';

Changed from:

import Yoga from '@react-pdf/yoga';

If I am changing the code manually in the node_modules folder to the old import and also add a yoga folder (that was created in npm installations in the past) to node_modules@react-pdf I am able to compile the project again.

Does anybody has an idea how to fix the problem permanently? I was not able to make the changes permanent via patch-package. The patch package module was only able to patch the changes for the files in the folder node_modules@react-pdf\layout\lib but did not work for creating the yoga folder in @react-pdf\ .


Solution

  • This is due to recent updates as you mentioned. You can see similar problems here. I am not sure why the problem is accruing but in my case, for now I locked the version to previous ones. More specifically you can have these in your package.json:

    "dependencies": {
       ....
       "@react-pdf/renderer": "3.1.14",
       "@react-pdf/layout": "3.6.4",
       ....
    }
    

    Note that you need to specify @react-pdf/layout in these dependencies to force the version. Also use 3.*.* instead of ^3.*.* to lock the version.