techniq/odata-query

ESM default export is not properly defined

Dzieni opened this issue · 0 comments

I have a a following buildQueryTest.mjs:

import buildQuery from "odata-query";

console.log(buildQuery({ top: 5 }));

It fails the following way:

michal@dzieni dataverse-poc % node buildQueryTest.mjs 
file:///Users/michal/Projects/dataverse-poc/buildQueryTest.mjs:3
console.log(buildQuery({ top: 5 }));
            ^

TypeError: buildQuery is not a function
    at file:///Users/michal/Projects/dataverse-poc/buildQueryTest.mjs:3:13
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:541:24)
    at async loadESM (node:internal/process/esm_loader:83:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.6.0

Workaround is to use a following piece of code:

import _buildQuery from "odata-query";
const buildQuery = _buildQuery.default;

console.log(buildQuery({ top: 5 }));

Although it is incorrect according to what TS types say:
image

To sum it up: to properly import odata-query in ESM-powered app with TypeScript, I have to perform following gymnastics:

import _buildQuery from 'odata-query'
const buildQuery = (_buildQuery as unknown as {default: typeof _buildQuery}).default