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!
Question:
How can I fake a real module via dynamic loading using that real module shape: values AND TYPES?
