Jest is not able to find the package canvas in dependencies even though it is present. yarn run test is failing with the following error.
packages/first/src/layouts/EventsTable.spec.tsx
● Test suite failed to run
Cannot find module '../build/Release/canvas.node'
Require stack:
- /Users/abc/Documents/monorepo/node_modules/canvas/lib/bindings.js
- /Users/abc/Documents/monorepo/node_modules/canvas/lib/canvas.js
- /Users/abc/Documents/monorepo/node_modules/canvas/index.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/utils.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/events/MouseEvent-impl.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/generated/MouseEvent.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/aborting/AbortSignal-impl.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/generated/AbortSignal.js
- /Users/abc/Documents/monorepo/node_modules/jsdom/lib/jsdom/living/generated/AddEventListenerOptions.js
......
Canvas is installed and present in the dependency list.
yarn why canvas
└─ pdfjs-dist@npm:3.11.174
└─ canvas@npm:2.11.2 (via npm:^2.11.2)
Tried removing all packages rm -rf node_modules and reinstalled, the problem still persists.
Node version - v18.16.1
yarn version - v3.2.3
Try to check if
canvaswas actually installed and exists in node_modules. (for me it was not installed and bothpdfjs-distandreact-pdfdidn't complain.)Running
npm ciwas working fine for me but havingnpm ci --ignore-scriptswas causing (silent) problems as scripts are required to make it work.npm rebuild canvascould be used afternpm ci --ignore-scriptsto fix that problem.btw: when trying to install it manually by
npm install canvasI got an error and has to apply manual command described here first: https://github.com/Automattic/node-canvas?tab=readme-ov-file#installation
and then installing canvas worked and tests passed.
if you are running into similar problem on Jenkins (using docker and alpine image) you might check this page (which helped me):
https://github.com/node-gfx/node-canvas-prebuilt/issues/77#issuecomment-884365161