Starlord
Starlord2mo ago

All packages installed on client side?

i dont quite get how to use nodejs runtime for action functions. how to install packages in convex. lets say i use external packages. this means i need to install the package that i need in convex but dont need on client side on client?
39 Replies
ballingt
ballingt2mo ago
Just installing something in an app (so adding it to package.json's dependencies field) doesn't add it to your client app. Generally you use a bundler that looks at which files are actually imported and those are the ones that end up in your client app. check out https://docs.convex.dev/functions/bundling which answers some of these
Bundling | Convex Developer Hub
Bundling is the process of gathering, optimizing and transpiling the JS/TS
ballingt
ballingt2mo ago
because "externalPackages": ["*"] would need me to install the package on client side
instead of thinking of it as "install something onthe client side" (you install things in package.json projects) think of it as installing in the project
Starlord
StarlordOP2mo ago
ok so vitejs will not budle those packages
ballingt
ballingt2mo ago
not unless you use them in your website
Starlord
StarlordOP2mo ago
ok understand thanks
ballingt
ballingt2mo ago
just like how vite will not bundle "convex/server" since you can't import that from a website
Starlord
StarlordOP2mo ago
so lets say i need to add this to convex : https://github.com/grammyjs/grammY i need nodejs runtime there right?
GitHub
GitHub - grammyjs/grammY: The Telegram Bot Framework.
The Telegram Bot Framework. Contribute to grammyjs/grammY development by creating an account on GitHub.
ballingt
ballingt2mo ago
I don't know that package, possibly does it say it's a Node.js library?
Starlord
StarlordOP2mo ago
its typescript package doesnt require node
ballingt
ballingt2mo ago
it all depends on if it uses Node.js apis like fs.readFileSync if it doesn't, then no you don't need to use Node.js
Starlord
StarlordOP2mo ago
ok so i can import it in normal mutation?
ballingt
ballingt2mo ago
if you have suggestions for how to improve https://docs.convex.dev/functions/actions would love to hear them, we try to lay this out there
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
Starlord
StarlordOP2mo ago
ok thanks
ballingt
ballingt2mo ago
say more You don't really import things in mutaitons, you import things in files, and you can define mutations in those files
Starlord
StarlordOP2mo ago
yes i mean import in file that is using actions
ballingt
ballingt2mo ago
if I read this as "can you import that library in the same file where you define a mutation" then yes
Starlord
StarlordOP2mo ago
npm package can be used only in actions not mutations right?
ballingt
ballingt2mo ago
interesting, where did you get that idea? just so we can help adress our docs
Starlord
StarlordOP2mo ago
i see import example only here "use node"; import { action } from "./_generated/server"; import SomeNpmPackage from "some-npm-package"; export const doSomething = action({ args: {}, handler: () => { // do something with SomeNpmPackage }, });
ballingt
ballingt2mo ago
files in the convex/ directory can define mutations, queries, and actions. From thes files you can import npm packages. SOme npm packages use Node.js-exclusive APIs, like fs.readFile. If you import these, then the bundler will throw an error.
Starlord
StarlordOP2mo ago
so mutation can use npm package
ballingt
ballingt2mo ago
yeah
Starlord
StarlordOP2mo ago
ok great thanks
ballingt
ballingt2mo ago
but mutations and queries can't make network requests
Starlord
StarlordOP2mo ago
yes got it
ballingt
ballingt2mo ago
so you can import, but you won't be able to run e.g. a Telegram bot there
Starlord
StarlordOP2mo ago
ok thanks
ballingt
ballingt2mo ago
@Starlord can I ask where you've learned about COnvex so far, so we can think about improving things? Was this from reading that page about actions? for misconceptions we need to get better at educating folks about, like thinking maybe mutations cant' use npm packages
Starlord
StarlordOP2mo ago
actions, runtimes, bundling
ballingt
ballingt2mo ago
cool, ok thanks
Starlord
StarlordOP2mo ago
in runtimes its not clear that convex runtime can use npm i thought it can use V8 only
ballingt
ballingt2mo ago
what does "use v8 only" mean like only browser-style APIs?
Starlord
StarlordOP2mo ago
V8 default functions yes
ballingt
ballingt2mo ago
got it most of the things you're thinking of probably aren't V8 default functions, stuff like fetch() but that's a small detail thanks this is all helpful
Starlord
StarlordOP2mo ago
another thing that is not related to the question. can i initialize global parameter with a data from the database that will remain same all the time. because this data doesnt change and i need it everywhere?
ballingt
ballingt2mo ago
"all the time" as in between requests?
Starlord
StarlordOP2mo ago
Yes
ballingt
ballingt2mo ago
No, every request is a new environment, you can't count on data sticking around from one mutation to another, or even one action to another.
Starlord
StarlordOP2mo ago
I understand well i have a problem that i still need to find out way to have item id - item name mapping in my code. nearly every function is using this mapping from the database. those are not needed database requests all the time. the only way seems is to create mapping in code would be great to have some "pregenerated" dynamic code that is used by all convex instances too bad there is action cache but no query cache

Did you find this page helpful?