holden
holden16mo ago

Import warning since upgrading Convex

After upgrading from Convex 1.0.3 to 1.3.1, I'm getting an import error:
- warn ../../node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/Users/matt/Dev/whatsnext/chart-studio/node_modules/node-fetch/lib'

Import trace for requested module:
../../node_modules/node-fetch/lib/index.js
../../node_modules/convex/dist/esm/browser/http_client-node.js
../../node_modules/convex/dist/esm/browser/index-node.js
./src/utils/convex.ts
./src/app/project/[id]/page.tsx
- warn ../../node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in '/Users/matt/Dev/whatsnext/chart-studio/node_modules/node-fetch/lib'

Import trace for requested module:
../../node_modules/node-fetch/lib/index.js
../../node_modules/convex/dist/esm/browser/http_client-node.js
../../node_modules/convex/dist/esm/browser/index-node.js
./src/utils/convex.ts
./src/app/project/[id]/page.tsx
I'm using a helper to setup Convex+Clerk and calling it from a NextJS app router page on the server (was previously working fine):
import { auth } from "@clerk/nextjs";
import { ConvexHttpClient } from "convex/browser";

/**
* Return a ConvexHttpClient that can be used in server-side contexts.
*/
export async function getConvex(): Promise<ConvexHttpClient> {
const { userId, getToken } = auth();
if (!userId) {
// Clerk middleware should handle this for us.
throw new Error("Unexpected: user not signed in");
}

const token = await getToken({
template: "convex",
});

if (!token) {
throw new Error("Unexpected: failed to get Clerk token");
}

const url = process.env.NEXT_PUBLIC_CONVEX_URL;
if (!url) {
throw new Error("Unexpected: NEXT_PUBLIC_CONVEX_URL not found: ");
}

const client = new ConvexHttpClient(url);
client.setAuth(token);

return client;
}
import { auth } from "@clerk/nextjs";
import { ConvexHttpClient } from "convex/browser";

/**
* Return a ConvexHttpClient that can be used in server-side contexts.
*/
export async function getConvex(): Promise<ConvexHttpClient> {
const { userId, getToken } = auth();
if (!userId) {
// Clerk middleware should handle this for us.
throw new Error("Unexpected: user not signed in");
}

const token = await getToken({
template: "convex",
});

if (!token) {
throw new Error("Unexpected: failed to get Clerk token");
}

const url = process.env.NEXT_PUBLIC_CONVEX_URL;
if (!url) {
throw new Error("Unexpected: NEXT_PUBLIC_CONVEX_URL not found: ");
}

const client = new ConvexHttpClient(url);
client.setAuth(token);

return client;
}
Anything I need to change after upgrading?
4 Replies
presley
presley16mo ago
Hmm.. it seems you are running older version of Node.js, where fetch() is not provided and we try to import node-fetch, but some if its dependencies are not installed (which might mean we have not declared it as dependency). Sorry about the trouble. As a workaround, can you try either upgrading your node version to Node 18 or npm installing node-fetch? Hmm.. we do declare node-fetch as dependency, so perhaps you need to run npm install?
ballingt
ballingt16mo ago
@holden This looks like an issue with Next.js module resolution and 'encoding' being an optional depenency of node-fetch, like https://github.com/snowflakedb/snowflake-connector-nodejs/issues/609 and some related issues describe. We'll look into this, for now can you try installing `encoding' https://www.npmjs.com/package/encoding and a dev dependency, the workaround suggested in these issues?
GitHub
SNOW-889050: NextJS Weird Warning Message · Issue #609 · snowflaked...
Summary Working with an NX monorepo with NextJS. I'm able to query the database, but grabbing a weird warning message when I launch the application. Figured it might be worth flagging, sorry if...
npm
encoding
Convert encodings, uses iconv-lite. Latest version: 0.1.13, last published: 3 years ago. Start using encoding in your project by running npm i encoding. There are 725 other projects in the npm registry using encoding.
ballingt
ballingt16mo ago
This should just be a warning message, everything should work fine, so another option is ignoring it. I'll look at what workaround these other libraries are using to avoid this warning.
holden
holdenOP16mo ago
ah ok, thanks. yeah, everything seems to be working fine, so maybe just a harmless warning and not specific to convex. i believe i'm using latest node. i'll just ignore for now, and hope someone fixes upstream of you, thanks!

Did you find this page helpful?