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
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
because "externalPackages": ["*"] would need me to install the package on client sideinstead 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
ok so vitejs will not budle those packages
not unless you use them in your website
ok understand thanks
just like how vite will not bundle
"convex/server"
since you can't import that from a websiteso 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.
I don't know that package, possibly
does it say it's a Node.js library?
its typescript package
doesnt require node
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.jsok so i can import it in normal mutation?
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
ok thanks
say more
You don't really import things in mutaitons, you import things in files, and you can define mutations in those files
yes i mean import in file that is using actions
if I read this as "can you import that library in the same file where you define a mutation" then yes
npm package can be used only in actions not mutations right?
interesting, where did you get that idea? just so we can help adress our docs
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
},
});
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.so mutation can use npm package
yeah
ok great thanks
but mutations and queries can't make network requests
yes got it
so you can import, but you won't be able to run e.g. a Telegram bot there
ok thanks
@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
actions, runtimes, bundling
cool, ok thanks
in runtimes its not clear that convex runtime can use npm
i thought it can use V8 only
what does "use v8 only" mean
like only browser-style APIs?
V8 default functions
yes
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 helpfulanother 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?
"all the time" as in between requests?
Yes
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.
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