Open stewones opened 4 months ago
Hey @stewones, great to see you around here again! Ran into another tricky spot, huh? Let's dive into it. 🚀
The "ReferenceError: Cannot access uninitialized variable" error you're encountering when trying to generate a binary from an app that relies on langchainjs using Bun is likely due to the way Bun handles module exports and imports, especially when dealing with circular dependencies or the order of module initialization.
In JavaScript and Node.js environments, modules are loaded and evaluated synchronously. This means that if a module A exports something that is immediately required by module B, which in turn is required by module A (a circular dependency), the environment ensures that these dependencies are resolved in a way that does not throw a reference error, even if it means one of the modules might temporarily get an incomplete view of the other module's exports.
However, Bun, being a newer and different runtime that aims for performance improvements over Node.js, might handle these situations differently. It could be stricter about the order of module initialization or how circular dependencies are resolved. If Bun encounters a reference to an export that has not been initialized yet due to the module containing it not having been fully evaluated, it might throw a "ReferenceError: Cannot access uninitialized variable" error.
Given the extensive list of exports in your entrypoints.js
file, it's plausible that there's a circular dependency somewhere within the langchain
modules, or a module is being imported before it's fully initialized due to the order of exports. This situation is exacerbated in a complex dependency graph where many modules interdepend on each other.
To resolve this issue, you might need to:
langchain
modules. Look for circular dependencies that might cause some modules to be used before they're fully initialized.Without specific details on the internals of the langchain
modules and how they interact, these are general approaches to troubleshooting and resolving the "ReferenceError: Cannot access uninitialized variable" error in this context.
Unfortunately, I don't have the exact structure of the dependency graph within the langchainjs codebase. This information might be available in the repository's documentation or by analyzing the codebase with a tool that can generate dependency graphs. If you need further assistance, please provide more details about the specific modules and dependencies involved in your application.
Thanks for the heads up - stopped the Bun CI test due to some issues but will try to fix this and get that back running.
@stewones have you found a way around this? I'm building a TS project with Bun for Lambda in 'esm' and for target platform 'node', all looks good except:
file:///Users/user/Development/.../packages/lambda-processor/dist/index.js:758712
class BaseStringPromptTemplate extends BasePromptTemplate {
^
ReferenceError: Cannot access 'BasePromptTemplate' before initialization
at file:///Users/user/Development/.../packages/lambda-processor/dist/index.js:758712:40
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v18.17.0
@jacoblee93 Hey Jacob 👋🏼 any chance this can get solved? PromptTemplates can't really be substituted if used with OpenAI's function calling, correct?
No I haven't found a workaround. Anyways I was just experimenting with bun binaries. My workload runs with "bun run" in production.
Too bad you getting this on windows 🥹
Not on Windows but in a Lambda function, even worse
I have it planned for two weeks out but will try to get to it as soon as I can. Sorry for the delay.
is there any update regarding this issue?
Same issue happens for me when I try to build with bun.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
I'm trying to generate a binary from an app that relies on langchain.
here's the minimal reproduction https://github.com/stewones/langchain-bun-binary
steps to recreate:
bun install
bun build --compile --sourcemap ./index.ts --outfile app
./app
System Info
platform: mac Python: 3.9.6 langchain: 0.1.25