In the javascript node I am getting the error
PLease understand the below code :
function encodeMimeMessageAsBase64Url(mimeContent) {
// Ensure Buffer is available in your execution environment.
// In Voiceflow's JavaScript steps, Buffer might not be available directly.
// You might need to use browser-compatible methods like btoa and TextEncoder if this causes issues.
const base64url = Buffer.from(mimeContent)
.toString('base64')
.replace(/+/g, '-') // Convert '+' to '-'
.replace(///g, '') // Convert '/' to ''
.replace(/=+$/, ''); // Remove trailing '='
return base64url;
}
// --- Example Usage ---
// Compose MIME text email manually
mimeMessage =
Content-Type: text/plain; charset="utf-8"
To: umairperwaizmalik1@gmail.com
From: cxboltagency@gmail.com
Subject: Test Subject
Hello Umair, this is a test email from Voiceflow!
;
// Encode message using the new function
rawMessage = encodeMimeMessageAsBase64Url(mimeMessage);
// In Voiceflow, you would typically assign 'rawMessage' to a variable
// that you've defined in your step, for example:
// _.set('rawMessageVariable', rawMessage);
if using _
for state management,
// or simply if rawMessage
is an output variable of your JavaScript step.
console.log(rawMessage); // For testing outside Voiceflow to see the output
Task:
Please help me resolve the error3 Replies
It’s failing because the Buffer class is a Node-only global; the JavaScript Code step in Voiceflow runs in a sandboxed V8 runtime where Buffer isn’t exposed.
Replace the Buffer logic with a browser-compatible approach that Voiceflow’s runtime does support (
•
• We manually transform the result into base64-url, which Gmail’s API expects. After this step the Voiceflow variable (
Replace the Buffer logic with a browser-compatible approach that Voiceflow’s runtime does support (
TextEncoder
+ btoa
).
Drop this inside your JavaScript step (the variable mimeMessage
must already exist in Voiceflow before this step runs):
What changed
• TextEncoder
is part of the standard ES API and is available in Voiceflow’s V8 runtime.•
btoa
converts a binary string to base64.• We manually transform the result into base64-url, which Gmail’s API expects. After this step the Voiceflow variable (
rawMessageVar
above) contains the correctly‐encoded value ready to pass to the Gmail API.
That should clear the “Buffer is not defined” error.const utf8Bytes = new TextEncoder().encode(mimeMessage);
let binary = '';
for (let i = 0; i < utf8Bytes.length; i++) {
binary += String.fromCharCode(utf8Bytes[i]);
}
/* 2. binary → standard base64 /
let base64 = btoa(binary);
/ 3. base64 → base64-url (RFC 4648 §5) /
const rawMessage = base64
.replace(/+/g, '-') // “+” → “-”
.replace(///g, '') // “/” → “”
.replace(/=+$/, ''); // trim “=” padding
/ 4. save result to a Voiceflow variable that already exists */
rawMessageVar = rawMessage; // <-- change to your output var name
I am executing the above code. but still I am getting error.
Variables are defined in the voiceflow but still there is the error
You can't use Buffer. In whatever you used to write that code, say that it will be run in the "V8 Isolate" runtime.