Comments (10)
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.
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.
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.
Is there a known workaround to use Client class in an ESM module?
from faunadb-js.
Internal ticket number is FE-2896
from faunadb-js.
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.
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.
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.
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.
I got this to work in ESM code by using import faunadb from 'faunadb'
as my import.
from faunadb-js.
Related Issues (20)
- provide internal function to "normalize" "data" result
- Property 'requestResult' does not exist on type 'typeof FaunaHTTPError'.
- invalid argument: Regular Class Ref expected, Schema Class Ref provided. HOT 3
- "invalid ref" when using `client.query( Create( Collection('test') ...` HOT 3
- UDF's not showing javascript variables HOT 2
- process.version & process.versions fail build on Vercel Edge API Route HOT 3
- Failing Queries from Cancelled Stream Error HOT 5
- The package "http2" wasn't found on the file system but is built into node HOT 11
- Typechecking functions such as `IsString` should also null as a parameter (typescript typing issue) HOT 2
- 4.7.0 to 4.7.1 causes InvalidValue error when importing code HOT 5
- Is it safe to expose the key in the browser? HOT 3
- Explictly set 'Content-Type': 'application/json' HOT 1
- OPTIONS xhr request before POST HOT 1
- PageHelper.nextPage() fails to handle the final page HOT 2
- PageHelper.nextPage() throws an error if `size` parameter is passed to `Client.paginate()` HOT 1
- what is the status of the ESM driver? HOT 2
- require('util') fails in some ESM configurations HOT 1
- "TypeError: Incorrect type for the 'signal' field on 'RequestInitializerDict': the provided value is not of type 'AbortSignal'." HOT 1
- Allow login, disable creating account HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from faunadb-js.