Error initializing bundle containing user code. Common causes include imports of libraries that rely on browser or Node.js APIs, which do not exist in the V8 environment user code is evaluated in. See the provided stack trace for further information..
Error Parameters: {
"message": "TextEncoder is not defined",
"stacktrace": "ReferenceError: TextEncoder is not defined\n at Module.30121 (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:51728:17)\n at __webpack_require__ (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54749:42)\n at Object.96622 (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:41020:17)\n at __webpack_require__ (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54749:42)\n at Object.13261 (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:41005:14)\n at __webpack_require__ (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54749:42)\n at Object.94858 (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:40912:14)\n at __webpack_require__ (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54749:42)\n at Object.60835 (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:41491:21)\n at __webpack_require__ (/scratch/standalone/f332e8c3-d026-40cc-ab6a-994e0dd3a354/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54749:42)\n at (<isolated-vm boundary>)\n at /scratch/asset-install/a3c74a0ea403abb64ff702a77492b2bc/node_modules/@foundry/functions-typescript-runtime-lib/dist/isolate/Isolate.js:104:27"
}
ReferenceError: {Redacted message: see unsafe logs}
{Redacted stacktrace: see unsafe logs}
This is due to the runtime in functions being some custom runtime that the functions team created, which is either outdated or excludes specific node libraries. While I know functions V2 will fix these issues, it is not production-ready. It’s missing support to external sources and other key features.
Why are we using the OSDK in functions? Because we are triggering actions in our partners’ Foundry stack. In this case a request for proposal.
This needs to get fixed. You can not leave developers without support for your own SDKs. While I might be able to work around this issue using compute modules, I am very frustrated to have to resort to this.
TypeScript v1 functions are executed in a minimal V8 environment which does not have access to Node.js APIs. You can, however, import a polyfill for TextEncoder and add it to the global scope.
Add @zxing/text-encoding from the library sidebar in Authoring.
Before you instantiate your confidential OAuth client, update the global scope:
import { TextEncoder } from "@zxing/text-encoding";
import { Function } from "@foundry/functions-api";
export class MyFunctions {
@Function()
public myFunction(): string {
// Update the global scope
globalThis.TextEncoder = TextEncoder;
// ..
}
}
I get the same error. I actually tried to polyfill this myself and it looks like the error occurs when the enviornment spins up. I tried adding the polyfil in multiple places including the instance method and right after the import of TextEncoder and I get the same error.
Error initializing bundle containing user code. Common causes include imports of libraries that rely on browser or Node.js APIs, which do not exist in the V8 environment user code is evaluated in. See the provided stack trace for further information..
Error Parameters: {
"message": "TextEncoder is not defined",
"stacktrace": "ReferenceError: TextEncoder is not defined\n at Module.30121 (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:54853:17)\n at __webpack_require__ (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57874:42)\n at Object.96622 (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44126:17)\n at __webpack_require__ (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57874:42)\n at Object.13261 (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44111:14)\n at __webpack_require__ (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57874:42)\n at Object.94858 (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44018:14)\n at __webpack_require__ (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57874:42)\n at Object.60835 (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44616:21)\n at __webpack_require__ (/scratch/standalone/53823925-f289-4665-b3e6-a89420b75879/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57874:42)\n at (<isolated-vm boundary>)\n at /scratch/asset-install/a3c74a0ea403abb64ff702a77492b2bc/node_modules/@foundry/functions-typescript-runtime-lib/dist/isolate/Isolate.js:104:27"
}
ReferenceError: {Redacted message: see unsafe logs}
{Redacted stacktrace: see unsafe logs}
Can someone try reproducing the error with the OSDK and see where in this stack the error occurs?
import { TextEncoder, TextDecoder } from "@zxing/text-encoding";
import * as nodeEvents from 'events';
// attaches both global.Event and global.EventTarget
import EventTarget from '@ungap/event-target';
(globalThis as any).EventTarget = EventTarget;
(globalThis as any).TextEncoder = TextEncoder;
(globalThis as any).TextDecoder = TextDecoder;
(globalThis as any).EventEmitter = nodeEvents.EventEmitter;
export * from "./SemanticSearch";
export * from "./Text2Action";
export * from "./ComsOsdkFunctions";
export * from "./SalesForge";
Now I get this error which points to something missing in the deps chain:
Error initializing bundle containing user code. Common causes include imports of libraries that rely on browser or Node.js APIs, which do not exist in the V8 environment user code is evaluated in. See the provided stack trace for further information..
Error Parameters: {
"message": "Class extends value undefined is not a constructor or null",
"stacktrace": "TypeError: Class extends value undefined is not a constructor or null\n at Module.30121 (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:57515:21)\n at __webpack_require__ (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:58496:42)\n at Object.96622 (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44629:17)\n at __webpack_require__ (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:58496:42)\n at Object.13261 (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44614:14)\n at __webpack_require__ (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:58496:42)\n at Object.94858 (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:44521:14)\n at __webpack_require__ (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:58496:42)\n at Object.60835 (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:45128:21)\n at __webpack_require__ (/scratch/standalone/b97ee577-c330-4452-8095-91f65a674c18/code-assist/contents/functions-typescript/dist/Functions.bundle.js:58496:42)\n at (<isolated-vm boundary>)\n at /scratch/asset-install/a3c74a0ea403abb64ff702a77492b2bc/node_modules/@foundry/functions-typescript-runtime-lib/dist/isolate/Isolate.js:104:27"
}
TypeError: {Redacted message: see unsafe logs}
{Redacted stacktrace: see unsafe logs}
import { TextEncoder, TextDecoder } from '@zxing/text-encoding';
import { EventEmitter } from 'events';
import { EventTarget, Event } from "event-target-shim";
// put the foundational pieces on the global
(globalThis as any).Event = Event;
(globalThis as any).EventEmitter = EventEmitter;
(globalThis as any).TextEncoder = TextEncoder;
(globalThis as any).TextDecoder = TextDecoder;
(globalThis as any).EventTarget = EventTarget;
And it worked!!! Why can’t you apply the same pollyfills in the bundling process so developers like me don’t run into these errors in the future. Also being a little more proactive would be cool. It’s pretty clear you didn’t setup a test case for this and actually troubleshoot it on your side. I’m trying to help you fix your product do developers can have an awesome experience in Foundry. Posting half baked fixes shows you are not interested in doing the same.
Thanks for persevering through this issue and sorry for the initial bad polyfill recommendation.
Just for a bit of context (mostly for others that find this thread in the future):
The architecture of TypeScript Functions V1 (the current GA version) has a limited set of JS APIs. Functions V1 have their own way of calling the Ontology and generally do not require importing of the OSDK.
TypeScript OSDK requires some of these APIs, hence the need for polyfills.
Palantir is working on TypeScript Functions v2 (they are available in Beta right now), which work natively with OSDK as the way to interface with the ontology. We’re giving users the ability to get a full Node environment rather than the limited v1 environment. They currently miss the ability to call external systems and Marketplace support, both of which are actively being worked on. Once they ship and V2 exits Beta, it will be a wholesale replacement for V1 making your usecase natively supported.
We don’t want to bundle the polyfills in V1 or in OSDK, because the use case of calling OSDK from TypeScript Function v1 is quite niche (as I said above there is a functions v1 native way of interfacing with the ontology) and adding polyfills will impact the performance for everyone using them.
That said our documentation was definitely lacking here. We’re going to do the following:
Leave this post up on Community, hopefully other developers that need help will find your resolution helpful.
Modify our documentation to explain polyfill usage and this specific case.
Clarify the differences between Typescript Functions V1 and V2