caxa icon indicating copy to clipboard operation
caxa copied to clipboard

default export does not work as expected

Open renkei opened this issue 4 years ago • 3 comments
trafficstars

In a *.js file (CommonJS)

const caxa = require('caxa')
caxa(...).catch(err => console.log(err.message))

results in: ERROR, caxa is not a function

The same is true for a *.mjs (ESM) file with import syntax.

Instead, this works for me:

const caxa = require('caxa').default
caxa(...).catch(err => console.log(err.message))

renkei avatar Mar 29 '21 15:03 renkei

Yeah, I’ve noticed this before. I played around with different tsconfig.json fields and different ways to export from TypeScript, and I Googled around, but found no way to fix this. Do you know what’s wrong?

leafac avatar May 02 '21 10:05 leafac

I encountered the same issue while using the ESM import syntax

import caxa from 'caxa';

await caxa(...)

results it ERROR: TypeError: caxa is not a function

The workaround is:

import caxa from 'caxa';

await caxa.default({
...
})

It seemes that caxa, instead of being a function, it's an object with a default property which is the actual function

import caxa from 'caxa'

console.log(caxa);

resulted in { default: [AsyncFunction: caxa] }.

Hope these information could help you figure out which may be the issue

matteosacchetto avatar Dec 21 '21 17:12 matteosacchetto

Thanks for the information.

I learned that there are two solutions to this:

  1. Transform caxa into an ESM-only package. This is the approach I’ll be taking soon.
  2. Create a dummy file that re-exports caxa.default. This is mostly for backwards compatibility with RequireJS.

leafac avatar Dec 30 '21 15:12 leafac

Hi y’all,

Thanks for using caxa and for the conversation here.

I’ve been thinking about the broad strategy employed by caxa and concluded that there is a better way to solve the problem. It uses ESM, but it doesn’t include a programmatic API, it’s CLI-only.

It’s a different enough approach that I think it deserves a new name, and it’s part of a bigger toolset that I’m building, which I call Radically Straightforward · Package.

I’m deprecating caxa and archiving this repository. I invite you to continue the conversation in Radically Straightforward’s issues.

Best.

leafac avatar Nov 21 '23 15:11 leafac