Jczhang
Jczhang15mo ago

Convex Action build error: could not resolve 'crypto-js'

I tried to import CryptoJS from "crypto-js"; in a custom convex action But on npx convex dev I got this error (which I think comes from esbuild but I am not familiar with what is happening underlying Convex)
You can mark the path "crypto-js" as external to exclude it from the bundle, which will remove
this error.
You can mark the path "crypto-js" as external to exclude it from the bundle, which will remove
this error.
How can I mark this dep as external in my next.js project? I have installed the package from npm install. Thanks for helping!
6 Replies
ian
ian15mo ago
Is this what you're looking for? https://docs.convex.dev/functions/bundling#external-packages Putting
{
"node": {
"externalPackages": ["crypto-js"]
}
}
{
"node": {
"externalPackages": ["crypto-js"]
}
}
in convex.json at the root of your project?
Bundling | Convex Developer Hub
Bundling is the process of gathering, optimizing and transpiling the JS/TS
ian
ian15mo ago
Jczhang
JczhangOP15mo ago
Hi @ian this is helpful thanks you! However I don't have a convex.json file and I tried to create it manually in my next.js project. I tried both to create it under root folder or the ./convex/ folder. But I am still getting the same error (Could not resolve "crypto-js" You can mark the path "crypto-js" as external to exclude it from the bundle, which will remove this error.). My convex.json
{
"node": {
"externalPackages": ["*"] // change to "crypto-js" still doesn't work
}
}
{
"node": {
"externalPackages": ["*"] // change to "crypto-js" still doesn't work
}
}
Additional thoughts: 1. Thanks for this pointer and part of my bad that I never checked this bundling tutorial on docs site. I was checking the "Actions" tab to find solutions. Maybe if possible we can add a link from "Actions" page to the "Bundling" page 2. Regarding web crypto API on default runtime, good suggestion though. I tried AES-GCM there and got a Not Implemented error. Therefore I tried to use node runtime instead. Also using a npm pkg is easier for me to implement the encryption. Hi sorry about this I figured out that after the external pkg fix it should work. The way I was importing "crypto-js" was wrong. This is resolved. And for the addition thoughts I think it should be still useful to share.
ian
ian15mo ago
Thank you for following up! What did the correct code look like in the end? Sorry it was hard to figure out- good idea to link to bundling. Any other ideas for improvements?
Twine
Twine4mo ago
What if the crypto was installed in your dotenv package, and the attached is what you get, how do I resolve this?
No description
ballingt
ballingt4mo ago
You shouldn't use dotenv from convex packages, it's a Node.js library. What are you trying to use it for?

Did you find this page helpful?