Tom Redman
Tom Redman5d ago

"use node" is present in node runtime file but still getting "you are using node APIs" error

In a file in which I only use actions, I'm trying to use the mailchimp npm package:
// mailchimpActions.ts
"use node";
import Mailchimp from "./mailchimpNode";
...
// mailchimpActions.ts
"use node";
import Mailchimp from "./mailchimpNode";
...
I've tried: - putting "use node"; at the top of the file, at the top of the imported file, and every variation thereof - putting all the Mailchimp import and classes I've created in the same file - No other files import the mailchimp node package - Deleting the _generated folder and re-building - I've added the mailchimp package as an external dependency in convex.json, and then tried * to no avail:
{
"node": {
"externalPackages": [
"*"
]
}
}
{
"node": {
"externalPackages": [
"*"
]
}
}
Yet I'm still getting this error:
convex | ✘ [ERROR] Could not resolve "querystring"
convex |
convex | node_modules/@mailchimp/mailchimp_marketing/src/ApiClient.js:18:26:
convex | 18 │ var querystring = require('querystring');
convex | ╵ ~~~~~~~~~~~~~
convex |
convex | The package "querystring" wasn't found on the file system but is built into node. Are you trying
convex | to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
✖ It looks like you are using Node APIs from a file without the "use node" directive.
convex | See https://docs.convex.dev/functions/runtimes#nodejs-runtime
convex | ✘ [ERROR] Could not resolve "querystring"
convex |
convex | node_modules/@mailchimp/mailchimp_marketing/src/ApiClient.js:18:26:
convex | 18 │ var querystring = require('querystring');
convex | ╵ ~~~~~~~~~~~~~
convex |
convex | The package "querystring" wasn't found on the file system but is built into node. Are you trying
convex | to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
✖ It looks like you are using Node APIs from a file without the "use node" directive.
convex | See https://docs.convex.dev/functions/runtimes#nodejs-runtime
Here is my Mailchimp file:
"use node";

import mailchimp from "@mailchimp/mailchimp_marketing";
import type { Infer } from "convex/values";
import type { mailchimpCredentialsSchema } from "../../schemas/integrationCredentialsSchema";

class Mailchimp {
private api: any;
constructor({
credentials,
}: {
credentials: Infer<typeof mailchimpCredentialsSchema>;
}) {
this.api = mailchimp;
this.api.setConfig({
apiKey: credentials.credentials.accessToken,
server: credentials.credentials.serverPrefix,
});
}

async getLists() {
return await this.api.lists.getAllLists({
includeTotalContacts: true,
});
}
}

export default Mailchimp;
"use node";

import mailchimp from "@mailchimp/mailchimp_marketing";
import type { Infer } from "convex/values";
import type { mailchimpCredentialsSchema } from "../../schemas/integrationCredentialsSchema";

class Mailchimp {
private api: any;
constructor({
credentials,
}: {
credentials: Infer<typeof mailchimpCredentialsSchema>;
}) {
this.api = mailchimp;
this.api.setConfig({
apiKey: credentials.credentials.accessToken,
server: credentials.credentials.serverPrefix,
});
}

async getLists() {
return await this.api.lists.getAllLists({
includeTotalContacts: true,
});
}
}

export default Mailchimp;
Any ideas?
5 Replies
Convex Bot
Convex Bot5d ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
erquhart
erquhart5d ago
Are you importing that class into a file that doesn't have "use node"? Has to be node all the way down.
ballingt
ballingt5d ago
Often this is because another file in the convex directory is importing the node library (mailchimp) or another file in the convex directory imports this mailchimpActions file
Tom Redman
Tom RedmanOP5d ago
Yeah I have node in all of them. Thank you both! @Tom i will tripppple check – I suspect this is the culprit. Wha'ts really weird is that this was all working –– I was using this package for the last few months. But also, to be fair, the mailchimp package is a bit of a mess and is not typescript, so I've been cajoling it to work. To get a better experience overall, I just generated a new schema & client from an openAPI spec and a created my own client for it. 🙂 Thanks so much guys OH I guess it'd be ideal to have that error message show the import trace, if possible. E.g., "a node runtime file (mailchimp.ts) is being imported by a non-node runtime bundle: myCrazyMutations.ts"
ballingt
ballingt3d ago
Yeah I want to do this! We'd try bundling each file in turn to see which ones hit it

Did you find this page helpful?