1

I'm building a cross-platform app in Node.js + TypeScript that uses OpenCV. On Windows I use the original opencv4nodejs, and on Linux I use the actively maintained fork @u4/opencv4nodejs.

To avoid rewriting all imports across the codebase, I created a mediator module that conditionally loads one or the other:

// cv.ts type OpenCV = typeof import('opencv4nodejs'); let mod: any; try { mod = require('opencv4nodejs'); } catch { try { mod = require('@u4/opencv4nodejs'); } catch (err) { throw new Error('Cannot load OpenCV module: ' + err); } } const cv = mod as OpenCV; export default cv; 

So my wish was to rewrite my code to import from './cv' that would figure out the loading and give me the same shape of the module.

But this doesn't work with imports because the types of the namespace get lost!

enter image description here

Question:

How can I fake a real module via dynamic loading using that real module shape: values AND TYPES?

1 Answer 1

0

this is what worked:

// @ts-expect-error export = cv; export default cv; export type * from 'opencv4nodejs'; 

or a cleaner yet version:

export const { imwrite, Rect, Point2, FONT_HERSHEY_PLAIN } = cv; export default cv; export type * from 'opencv4nodejs'; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.