In my Vite TypeScript React application I’m trying to import the Opus Recorder library (https://www.npmjs.com/package/opus-recorder).
I have set this flag in tsconfig.json
:
"compilerOptions": {
"allowJs": true,
and imported this library in my index.tsx
in this way:
import * as opus from '../../../node_modules/opus-recorder/dist/recorder.min.js';
but when I try to invoke the constructor:
const recorder = new opus.Recorder({ encoderSampleRate: 44100 });
I get TypeError: opus.Recorder is not a constructor.
I also created a type declaration file in types/index.d.ts
:
declare module 'opus-recorder' {
// Recorder Configuration Interface
interface RecorderConfig {
bufferLength?: number;
encoderApplication?: number;
encoderFrameSize?: number;
encoderPath?: string;
encoderSampleRate?: number;
maxFramesPerPage?: number;
mediaTrackConstraints?: boolean | MediaTrackConstraints; // Allow flexible input
monitorGain?: number;
numberOfChannels?: number;
recordingGain?: number;
resampleQuality?: number;
streamPages?: boolean;
wavBitDepth?: number;
sourceNode?: { context: AudioContext | null } | MediaStreamAudioSourceNode;
}
// Recorder Class
class Recorder {
// Static Methods
static isRecordingSupported(): boolean;
// Constructor
constructor(config?: RecorderConfig);
// Instance Properties
state: "inactive" | "loading" | "recording" | "paused"; // Possible states
config: RecorderConfig;
encodedSamplePosition: number;
// Instance Methods (simplified for brevity)
initAudioContext(): void;
initWorklet(): Promise<void>;
initEncoder(): Promise<void>;
initialize: Promise<void>; // Combined promise for initWorklet & initEncoder
}
export default Recorder;
}
but I get the same error.
Can someone please help me?