danibeja
danibejaā€¢10mo ago

Importing Google API Libraries

@kapa.ai New to Convex and TS, so this might be a dumb question: I need to install these packages:
import { google } from 'googleapis';
import { OAuth2Client } from 'google-auth-library';
import { google } from 'googleapis';
import { OAuth2Client } from 'google-auth-library';
but when I do, I get requests to install all sorts of packages
The package "https" wasn't found on the file system but is built into node. Are you trying to
bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
The package "https" wasn't found on the file system but is built into node. Are you trying to
bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
If running locally, this would be resolved with a npm install googleapis google-auth-library but how can I install packages in my Convex project? Any help (or directing me to Docs I might be missing) is highly appreciated!
5 Replies
kapa.ai
kapa.aiā€¢10mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- In Convex, you can specify which packages used by Node actions should be installed on the server, instead of being bundled, via the node.externalPackages field in the convex.json file. This file should be in the root of your project, in the same directory as your package.json. To specify the packages you want to install, you can set the node.externalPackages field to an array of the package names. For your case, it would look like this:
{
"node": {
"externalPackages": ["googleapis", "google-auth-library"]
}
}
{
"node": {
"externalPackages": ["googleapis", "google-auth-library"]
}
}
This tells Convex to treat these packages as external dependencies, meaning they won't be bundled with your code but will be installed on the server when your code is pushed. The version installed matches the version installed in the node_modules folder on your local machine. Remember, this feature is currently exclusive to Convex's Node.js runtime and is in beta. If you encounter any issues, you can reach out to the Convex community on Discord for support. For more information, you can refer to the Convex documentation on specifying external packages (https://docs.convex.dev/functions/bundling#specifying-external-packages) and project configuration (https://docs.convex.dev/production/project-configuration#convex.json). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.aiā€¢10mo ago
<@917519543264477254> kapa.ai is still learning and improving, please let us know how it did by reacting below
danibeja
danibejaOPā€¢10mo ago
Thanks @kapa.ai that's helpful. I still don't see (even in those docs) how to deal with the tens of dependencies that those two packages need
āœ˜ [ERROR] Could not resolve "util"

node_modules/jwa/index.js:5:19:
5 ā”‚ var util = require('util');
āœ˜ [ERROR] Could not resolve "util"

node_modules/jwa/index.js:5:19:
5 ā”‚ var util = require('util');
I've seen (and tried using) use node at the top of the file. I've also tried adding platform: "node" I also have thought if it's possible to npm install into a node_modules folder inside my convex directory in my project... But at least the way I've tried doing all of that hasn't worked. Reading the docs isn't too clear to me, probably because of my inexperience with Node and TS, and Convex of course :/
ballingt
ballingtā€¢9mo ago
@danibeja "use node" is the way to go here since these libraries are intended for Node.js. The packages like "https" aren't packages, they're Node.js builtins. Do youu have any other files importing this file that don't use "use node"? That's the common issue here, "use node" applies to a file in the convex directory and all files it imports, but does not apply if another module imports it. We've got an issue to implement a linter warning or bundler error when a non-"use node" file imports a "use node" one since this will basically never work. Hm re deleted message, importing anything in the convex folder besides convex/_generated/api from a client component is very unusual and usually not allowed (since this would include unnecessary and potentially secret server code in your client bundle)
danibeja
danibejaOPā€¢9mo ago
Hey Tom! Thanks for the prompt response šŸ˜† I deleted that because your suggestion worked! I hadn't understood that I also had to remove the declaration of those packages as external from convex.json. Once I did that and "use node" things ran smoothly. But I wanted to confirm and test it before calling victory. Many thanks!

Did you find this page helpful?