Skip to content

Document Node.js image loading pattern to avoid imread() DOM error#109

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/fix-imread-referenceerror
Draft

Document Node.js image loading pattern to avoid imread() DOM error#109
Copilot wants to merge 4 commits intomainfrom
copilot/fix-imread-referenceerror

Conversation

Copy link
Contributor

Copilot AI commented Jan 22, 2026

cv.imread() throws ReferenceError: document is not defined in Node.js because it requires browser DOM APIs to access canvas/image elements.

Changes

  • README.md: Added "Loading Images" section documenting browser vs Node.js patterns
  • test/imread-nodejs.test.ts: Tests validating the Node.js approach and demonstrating correct usage patterns

Node.js Usage

Use Jimp (or similar) with matFromImageData() instead of imread():

const { Jimp } = require("jimp");
const cv = await require("@techstark/opencv-js");

// Load image with Jimp
const jimpImage = await Jimp.read("image.png");

// Convert to Mat
const img = cv.matFromImageData(jimpImage.bitmap);

// Process and cleanup
const gray = new cv.Mat();
cv.cvtColor(img, gray, cv.COLOR_RGBA2GRAY);
img.delete();
gray.delete();

See test/imread-nodejs.test.ts for complete working examples.

Browser environments continue using cv.imread('canvasId') unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>imread() throws error "abort(ReferenceError: document is not defined). Build with -s ASSERTIONS=1 for more info."</issue_title>
<issue_description>Based upon the node commonjs example:

const { getOpenCv } = require("./opencv.js");

async function main() {
  const { cv } = await getOpenCv();
  console.log("OpenCV.js is ready!");
  console.log(cv.getBuildInformation());
  const inputImage = cv.imread("Lenna.png");
  console.log("imread, done");
}

main();

Output:

(build info, as expected)
(a very long block of obfuscated code)

abort(ReferenceError: document is not defined). Build with -s ASSERTIONS=1 for more info.
(Use node --trace-uncaught ... to show where the exception was thrown)

Node.js v22.18.0
</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 22, 2026 05:06
Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com>
Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix imread() ReferenceError when using OpenCV.js Document Node.js image loading pattern to avoid imread() DOM error Jan 22, 2026
Copilot AI requested a review from ttt43ttt January 22, 2026 05:11
@ttt43ttt
Copy link
Contributor

@copilot remove the examples/node-image-loading.js. show the usage in the unit test, which is in node.js environment.

Co-authored-by: ttt43ttt <132509+ttt43ttt@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 27, 2026

@copilot remove the examples/node-image-loading.js. show the usage in the unit test, which is in node.js environment.

Done in 76bd2f8. Removed the example file and updated README to reference the unit test instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

imread() throws error "abort(ReferenceError: document is not defined). Build with -s ASSERTIONS=1 for more info."

2 participants