ashuvssut (ashu)
ashuvssut (ashu)16mo ago

Cannot resolve built-in Node's "crypto" library

apps/convex/utils.ts:5:19:
5 │ import crypto from "crypto";
╵ ~~~~~~~~

The package "crypto" 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.
apps/convex/utils.ts:5:19:
5 │ import crypto from "crypto";
╵ ~~~~~~~~

The package "crypto" 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.
I have put "use node" directive in utils.ts. still no effect. What am i doing wrong here?
5 Replies
ashuvssut (ashu)
ashuvssut (ashu)OP16mo ago
i tried to put "platform: 'node'" instead of "use node"... still no use
Michal Srb
Michal Srb16mo ago
Try to remove “use node” and the import line. We have a browser like crypto global. See https://docs.convex.dev/functions/runtimes#web-crypto-apis
Runtimes | Convex Developer Hub
Convex functions can run in two runtimes:
ashuvssut (ashu)
ashuvssut (ashu)OP15mo ago
I would migrate to that browser like crpyto global, but there will to much code to change. So i decided to stick to node's crypto This trick worked for me:- take out all the logics that use node's crypto as utility function into a separate file. doesn't matter if you have written the crypto module's import statement or not, if you have used those utility function in some other file, you have to put "use node"; directive this works only for Actions. this wont work for Query or Mutation because only Actions can use Node.js runtime and query mutation cant
ashuvssut (ashu)
ashuvssut (ashu)OP15mo ago
Example:- (Just to show a working demo code): https://github.com/ashuvssut/cookied/tree/dev/apps/convex
GitHub
cookied/apps/convex at dev · ashuvssut/cookied
Contribute to ashuvssut/cookied development by creating an account on GitHub.
nate
nate5mo ago
i see this error as well when i have a file that imports convex-test

Did you find this page helpful?