Giter Club home page Giter Club logo

Comments (10)

pieterdd avatar pieterdd commented on July 28, 2024 1

Hi Henry, thanks for helping me diagnose this issue. The first approach works on my local development server but yields the following error when deployed:

import { Client, query } from "faunadb";
         ^^^^^^
SyntaxError: Named export 'Client' not found. The requested module 'faunadb' 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 'faunadb';
const { Client, query } = pkg;

The second approach, which is also in line with what the error suggests, does not get accepted by TypeScript on my machine. The error reads:

Module '"[path to my code]/node_modules/faunadb/index"' has no default export.ts(1192)
index.d.ts(1, 1): 'export *' does not re-export a default.

from faunadb-js.

vxern avatar vxern commented on July 28, 2024 1

Are there any updates on this? Importing the library into an ESM project still errors.

  "dependencies": {
    "fauna": "npm:faunadb@^4.8.0"
  }
import * as Fauna from "fauna";
client: new Fauna.Client({
	secret: process.env.FAUNA_SECRET!,
	domain: "db.us.fauna.com",
	scheme: "https",
	port: 443,
}),
TypeError: Fauna.Client is not a constructor
    at createDatabase (c:\Users\Administrator\Documents\Programming\Projects\Private\logos\src\lib\database\database.ts:251:11)
    at createClient (c:\Users\Administrator\Documents\Programming\Projects\Private\logos\src\lib\client.ts:145:13)
    at initialiseClient (c:\Users\Administrator\Documents\Programming\Projects\Private\logos\src\lib\client.ts:168:17)
    at initialise (c:\Users\Administrator\Documents\Programming\Projects\Private\logos\src\initialise.ts:185:9)

from faunadb-js.

sumerokr avatar sumerokr commented on July 28, 2024 1

I use fauna with Cloudflare pages, therefore I have a /functions folder on root level of my project.
Had the same issue, but able to solve it with

// /functions/tsconfig.json
{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": ["esnext"],
    "types": ["@cloudflare/workers-types"],
    "moduleResolution": "Node" // THIS LINE ADDED
  }
}

And here is how I use fauna within my edge functions

import { Client, Ref, Collection, Get } from 'faunadb';

interface Env {
  FAUNA_DB_KEY: string;
}

export const onRequestGet: PagesFunction<Env> = async ({ request, env }) => {
  const faunaClient = new Client({
    secret: env.FAUNA_DB_KEY,
  });

  const url = new URL(request.url);
  const id = url.pathname.split('/').pop();

  const response: any = await faunaClient.query(Get(Ref(Collection('customers'), id)));

  return new Response(JSON.stringify(response));
};

Hope this could help someone.

from faunadb-js.

barancev avatar barancev commented on July 28, 2024 1

Is there a known workaround to use Client class in an ESM module?

from faunadb-js.

github-actions avatar github-actions commented on July 28, 2024

Internal ticket number is FE-2896

from faunadb-js.

henryfauna avatar henryfauna commented on July 28, 2024

Hi @pieterdd, sorry you ran into this. Do any of the following work for you?

import { Client } from 'faunadb'
new Client({
    secret: FAUNA_API_KEY,
    domain: 'db.us.fauna.com',
});
import faunadb from 'faunadb'
new faunadb.Client({
    secret: FAUNA_API_KEY,
    domain: 'db.us.fauna.com',
});

from faunadb-js.

ptpaterson avatar ptpaterson commented on July 28, 2024

Hi @pieterdd have you figured this out? I acknowledge it has been quite some time since you created the issue.

Have you tried setting the following in your tsconfig.json file?

    "esModuleInterop": true,
    "moduleResolution":"node",

from faunadb-js.

pieterdd avatar pieterdd commented on July 28, 2024

I haven't, so I added the lines to tsconfig.json. After deploying it this is still popping up in the logs:

TypeError: faunadb.Client is not a constructor

from faunadb-js.

pieterdd avatar pieterdd commented on July 28, 2024

FWIW, I bumped into vitejs/vite#4680. It looks like CommonJS modules and Vite (@rollup/plugin-commonjs) don't mix very well. The thread tracks with my earlier observation that it only occurs on my production builds.

from faunadb-js.

electrovir avatar electrovir commented on July 28, 2024

I got this to work in ESM code by using import faunadb from 'faunadb' as my import.

from faunadb-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.