Reputation: 512
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
Upvotes: 3
Views: 1992
Reputation: 1629
I am using macOS and the following command did the trick for me.
brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman python-setuptools
After which I cleared my node_modules
folder and reinstalled packages.
Upvotes: 3
Reputation: 11
Try to check if canvas
was actually installed and exists in node_modules.
(for me it was not installed and both pdfjs-dist
and react-pdf
didn't complain.)
Running npm ci
was working fine for me but having npm ci --ignore-scripts
was causing (silent) problems as scripts are required to make it work.
npm rebuild canvas
could be used after npm ci --ignore-scripts
to fix that problem.
btw: when trying to install it manually by
npm install canvas
I 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
Upvotes: 1