Bogdan
Bogdan16mo ago

Could not resolve "crypto"

I am trying to have a function that generates a code and returns it to the react client.
import crypto from 'crypto'
import { mutation } from "./_generated/server";

export const getSessionId = mutation({
args: {},
handler: async (ctx) => {
let sessionId = generateCode()
await ctx.db.insert("rooms", {sessionId: sessionId});
return sessionId
}
})

function generateCode() {
return crypto.randomBytes(3).toString('hex').toUpperCase()
}
import crypto from 'crypto'
import { mutation } from "./_generated/server";

export const getSessionId = mutation({
args: {},
handler: async (ctx) => {
let sessionId = generateCode()
await ctx.db.insert("rooms", {sessionId: sessionId});
return sessionId
}
})

function generateCode() {
return crypto.randomBytes(3).toString('hex').toUpperCase()
}
but then i get this error:
✘ [ERROR] Could not resolve "crypto"

src/convex/room.ts:1:19:
1 │ import crypto from 'crypto'
✘ [ERROR] Could not resolve "crypto"

src/convex/room.ts:1:19:
1 │ import crypto from 'crypto'
how would i resolve this? i also feel as though there is a gap in my understanding of how convex is supposed to work .
4 Replies
Web Dev Cody
Web Dev Cody16mo ago
you probably need to use an action and put "use node" at the top of the file?
Michal Srb
Michal Srb16mo ago
@Bogdan Convex's default environment is more similar to the browser (or Cloudflare workers), so crypto is a global, which has a subset of what's available in the browser: https://developer.mozilla.org/en-US/docs/Web/API/crypto_property So you can remove the import line and the rest will hopefully work as intended.
crypto global property - Web APIs | MDN
The global read-only crypto property returns the Crypto object associated to the global object. This object allows web pages access to certain cryptographic related services.
Bogdan
BogdanOP16mo ago
thanks for the help. who would have guessed the answer to my question was in the part of the docs i didnt read. the global crypto doesnt have the randomBytes method although i prolly could have worked around it. going to leave the solution code if anyone else looks for this issue
//roomactions.ts
'use node';

import crypto from 'crypto'
import { action } from "./_generated/server";
import { api } from "./_generated/api";

export const getSessionId = action({
args: {},
handler: async (ctx) => {
const sessionId = generateCode();
await ctx.runMutation(api.room.addSessionId, {
sessionId: sessionId
})
}
})

function generateCode() {
return crypto.randomBytes(3).toString('hex').toUpperCase()
}
//roomactions.ts
'use node';

import crypto from 'crypto'
import { action } from "./_generated/server";
import { api } from "./_generated/api";

export const getSessionId = action({
args: {},
handler: async (ctx) => {
const sessionId = generateCode();
await ctx.runMutation(api.room.addSessionId, {
sessionId: sessionId
})
}
})

function generateCode() {
return crypto.randomBytes(3).toString('hex').toUpperCase()
}
//room.ts
import { mutation } from "./_generated/server";
import { v } from "convex/values";

export const addSessionId = mutation({
args: { sessionId: v.string() },
handler: async (ctx, sessionId) => {
await ctx.db.insert("rooms", sessionId);
}
})
//room.ts
import { mutation } from "./_generated/server";
import { v } from "convex/values";

export const addSessionId = mutation({
args: { sessionId: v.string() },
handler: async (ctx, sessionId) => {
await ctx.db.insert("rooms", sessionId);
}
})
thanks for the help
lee
lee16mo ago
For posterity if you want to use the convex runtime, the workaround would be crypto.getRandomValues which is similar to node's randomBytes

Did you find this page helpful?