supabase / postgrest-js

Isomorphic JavaScript client for PostgREST.
https://supabase.com
MIT License
965 stars 129 forks source link

Cannot import postgres-js as an ESM module in node. #411

Open tomprince opened 1 year ago

tomprince commented 1 year ago

Bug report

Describe the bug

A clear and concise description of what the bug is.

To Reproduce

  1. Create a new package and install postgres-js:
    mkdir test-package && cd test-package
    echo '{}' > package.json
    npm install @supabase/postgrest-js@latest
  2. Create a module that tries to import a named export from postgrest-js:
    echo 'import { PostgrestClient } from "@supabase/postgrest-js"'  > test.mjs
  3. Try to run the module with node:
    node test.mjs
  4. Note the error trying to import a commonjs module.

Expected behavior

I expect the above import to work and import an ESM module.

Screenshots

❯ node test.mjs                                                           
file:///.../test.mjs:1
import { PostgrestClient } from "@supabase/postgrest-js"
         ^^^^^^^^^^^^^^^
SyntaxError: Named export 'PostgrestClient' not found. The requested module '@supabase/postgrest-js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@supabase/postgrest-js';
const { PostgrestClient } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)

Node.js v19.6.0

System information

Additional context

It appears that node only looks at the main entrypoint in package.json, and not module. It also specifies a newer and more complex way of specifying see here for details.

humphd commented 3 months ago

I'm hitting this over and over with postgrest-js trying to use it in different environments. To get my code to work, I have to use all 3 of these in different contexts:

// XXX: ncc wants
import * as postgrest from "@supabase/postgrest-js";

// XXX: esbuild wants:
import postgrest from "@supabase/postgrest-js";

// tsx wants:
import { PostgrestClient } from "@supabase/postgrest-js";

It would be great to be able to use this module without it breaking on import so much.