Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto is not defined #61

Open
its-dibo opened this issue Jan 18, 2024 · 10 comments
Open

crypto is not defined #61

its-dibo opened this issue Jan 18, 2024 · 10 comments

Comments

@its-dibo
Copy link

I got this error

 ReferenceError: crypto is not defined
0|mobarmegeen-backend  |     at hmac (node_modules/aws4fetch/dist/aws4fetch.cjs.js:223:21)
0|mobarmegeen-backend  |     at AwsV4Signer.signature (node_modules/aws4fetch/dist/aws4fetch.cjs.js:185:27)
0|mobarmegeen-backend  |     at AwsV4Signer.authHeader (node_modules/aws4fetch/dist/aws4fetch.cjs.js:177:34)
0|mobarmegeen-backend  |     at AwsV4Signer.sign (node_modules/aws4fetch/dist/aws4fetch.cjs.js:164:52)
0|mobarmegeen-backend  |     at AwsClient.sign (node_modules/aws4fetch/dist/aws4fetch.cjs.js:56:57)
0|mobarmegeen-backend  |     at AwsClient.fetch (node_modules/aws4fetch/dist/aws4fetch.cjs.js:69:40)
0|mobarmegeen-backend  |     at FilesCloudflareService.save (/apps/src/api/files/fs-providers/files.cloudflare.service.ts:54:8)
0|mobarmegeen-backend  |     at /apps/src/api/posts/posts.service.ts:135:10
0|mobarmegeen-backend  |     at matcher (/apps/src/api/files/files.utils.ts:47:14)
0|mobarmegeen-backend  |     at replaceAsync (/apps/src/api/files/files.utils.ts:90:9)
0|mobarmegeen-backend  |     at extractInlineData (/apps/src/api/files/files.utils.ts:52:7)
0|mobarmegeen-backend  |     at PostsService.saveFiles (/apps/src/api/posts/posts.service.ts:121:29)
0|mobarmegeen-backend  |     at PostsService.updateById (/apps/src/api/posts/posts.service.ts:45:17)
0|mobarmegeen-backend  |     at PostsController.update (/apps/src/api/posts/posts.controller.ts:104:25)
0|mobarmegeen-backend  |     at node_modules/@nestjs/core/router/router-execution-context.js:38:29
0|mobarmegeen-backend  |     at processTicksAndRejections (node:internal/process/task_queues:95:5)
0|mobarmegeen-backend  |     at async node_modules/@nestjs/core/router/router-execution-context.js:46:28
0|mobarmegeen-backend  |     at async Object.<anonymous> (node_modules/@nestjs/core/router/router-proxy.js:9:17)

the code:

let client = new AwsClient({
      accessKeyId: ***,
      secretAccessKey: ***!,
    });

let baseUrl =  `https://${accountId}.r2.cloudflarestorage.com`

client
      .fetch(`${baseUrl}/file.txt`, {
        method: 'put',
        body: 'test',
      })

I don't use crypto anywhere in my code
even installing crypto doesn't help npm i crypto

@mhart
Copy link
Owner

mhart commented Jan 18, 2024

Where are you running this? As the README says, this is "for environments that support fetch and SubtleCrypto"

@its-dibo
Copy link
Author

its-dibo commented Jan 29, 2024

Where are you running this? As the README says, this is "for environments that support fetch and SubtleCrypto"

in a Nodejs app which supports fetch

@mhart
Copy link
Owner

mhart commented Jan 29, 2024 via email

@its-dibo
Copy link
Author

Which Node.js version? Which OS?

Node v20
OS: ubuntu 22

@mhart
Copy link
Owner

mhart commented Jan 31, 2024

Not sure what's wrong with your env – it's defined for me:

$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> typeof crypto
'object'

@sino1641
Copy link

HI
I tried adding const crypto = require('crypto'); to the top of the code then it works.
I am day1 on nodejs using fedora with node output:

$ node
Welcome to Node.js v18.18.2.
Type ".help" for more information.
> typeof crypto
'object'

May it wouldbe of help.

@its-dibo
Copy link
Author

I tried adding const crypto = require('crypto');

this wo'nt work here because aws4fetch is an external package and we cannot modify its code base

@zdmc23
Copy link

zdmc23 commented Feb 29, 2024

@its-dibo , try adding the Vercel Edge Runtime Polyfill (https://edge-runtime.vercel.app/packages/ponyfill) package:

pnpm add @edge-runtime/ponyfill

then to use it:

import { crypto } from '@edge-runtime/ponyfill'

@luizzappa
Copy link

In case anyone is having this problem running Nuxt locally, you can create a plugin as follows:

export default defineNuxtPlugin(() => {
  const config = useRuntimeConfig();

  if (process.server && config.public.env.toLocaleLowerCase() !== 'prod') {
    const crypto = require('crypto');
    global.crypto = crypto;
  }
})

@1rwilson
Copy link

1rwilson commented Sep 5, 2024

I tried adding const crypto = require('crypto');

I am experiencing the same issue on both my local node.js environment and also when deploying on google cloud functions:
Screenshot 2024-09-05 at 20 08 44

Simply adding const crypto = require('crypto'); to the beginning of aws4fetch.cjs.js fixes the problem on my local environment. But I can't do this when deploying to GCF. Could this one line be added as per aws4.js ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants