usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
22.14k stars 1.04k forks source link

VMError: Cannot find module 'node:crypto' #1849

Open jcgsville opened 4 months ago

jcgsville commented 4 months ago

I'm trying to use a library in a Bruno Pre Request script that requires the global variables TextEncoder and crypto to be set.

I was able to set the global variable text encoder with the following snippet at the top of my script:

const { TextEncoder } = require("util")
global.TextEncoder = TextEncoder

When I try the same approach with the crypto library, it does not work

const crypto = require('node:crypto')
global.crypto = crypto

This addition results in the error: Error invoking remote method 'send-http-request': VMError: Cannot find module 'node:crypto'

The docs don't provide much detailed information about the JS runtime that bruno provides. It only mentions the crypto-js inbuilt library that it provides.

Any guidance on whether it is possible to use the node:crypto library?

PS I prepaid for Golden Edition and love Bruno. Thanks for all your work :)

helloanoop commented 4 months ago

@jcgsville You need to whitelist the crypto module

See https://github.com/usebruno/bruno/issues/306#issuecomment-1751487377

const crypto = require('node:crypto')

This must be const crypto = require('crypto')

Let me know if you are still facing issue.

I prepaid for Golden Edition and love Bruno. Thanks for all your work :)

Thank you for supporting Bruno @jcgsville !

jcgsville commented 3 months ago

Thanks @helloanoop ! That answer helped me get past that hurdle. (Small +1 for adding docs around this and other details of bruno.json. I wasn't able to find any 🙂)

It looks like crypto.subtle is undefined, which makes me think Bruno might be running the JS in a node environment Node 15 or older. crypto.subtle was introduced in Node 15.

https://nodejs.org/docs/latest-v15.x/api/webcrypto.html

I'm not seeing any information about the node version from which Bruno is executing scripts. Is it < Node 15? If >= Node 15, any thoughts on why crypto.subtle would be undefined?

Its-treason commented 3 months ago

Bruno runs on Electron 21 so Node 16, could a side effect of vm2 but I'm not sure. You can try my fork: https://github.com/Its-treason/bruno/releases/tag/nightly crypto.subtle is usable with it. My fork uses another script runtime and a newer Electron version.

derekjanni commented 3 months ago

having this problem on node v16.20.2 as well

I was able to import and use UUID in my pre-request script, however, to set some ids up for testing

const uuid = require("uuid");
bru.setVar('id1', uuid.v4());
bru.setVar('id2', uuid.v4());