strtok3 is an ECMAScript modules (ESM). In a CommonJS (CJS) project (it looks like that is what you have) can use dynamic import to load an ESM module.
(async () => {
const strtok3 = await import('strtok3');
})();
I will demonstrate how to do that. I use URLs instead of module names here, as I cannot import from local installed dependencies.
const strtok3 = 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
const token_types = 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
async function run() {
const {fromBuffer} = await import(strtok3);
const {UINT32_BE} = await import(token_types);
const testData = new Uint8Array([0x01, 0x00, 0x00, 0x00]);
const tokenizer = fromBuffer(testData);
const number = await tokenizer.readToken(UINT32_BE);
console.log(`Decoded number = ${number}`);
}
run().catch(err => {
console.error(`An error occured: ${err.message}`);
})
But you are using TypeScript, and gives additional challenge, as the TypeScript compiler does not respect the dynamic import, in CJS project.
import {loadEsm} from 'load-esm';
(async () => {
const strtok3 = await loadEsm<typeof import('strtok3')>('strtok3');
})();
As per StackOverflow policies I need disclose that I am the owner of all the used dependencies: strtok3, token-types and load-esm.
But rather then get this to work in your CJS project, it better to migrate your project to ESM. In your ESM project, you more easily load both ESM and CJS dependencies.