Open sabrenner opened 2 weeks ago
I realized that the workaround to disable ESM alltogether should be something like:
// register.mjs
import * as module from 'module'
module.register('import-in-the-middle/hook.mjs', import.meta.url, {
data: { exclude: [/langsmith/, /openai/] } // excluding openai as well, as there is another IITM issue with that
})
There still seems to be some problem with instrumenting LangChain properly, but I believe that is on me to figure out, otherwise I will follow up.
I am wondering in general if there are ways to accomplish this for the --loader
method, where we cannot leverage the kind of functionality in register.mjs
above, for "older" (back through 16) versions of Node. Additionally, any insight to the original issue is also appreciated, thanks! đ
I suspect the issue you are seeing is a duplicate of one of the existing known issues. So #38 or similar to #141.
import-in-the-middle
wraps modules in additional modules. It tries to create matching exports so the wrapping appears invisible but it is unable to fully replicate the behaviour of a module in a couple of cases.
Expected Behavior
Running
node --loader ./hook.mjs index.mjs
forshould not error.
Actual Behavior
When running
node --loader ./hook.mjs index.mjs
, the following error is produced:Steps to Reproduce the Problem
index.mjs
file, with the following contentshook.mjs
file, with the following contentsnode --loader ./hook.mjs index.mjs
in the current directory.Specifications
Running
npx envinfo
:Investigation
Context: I am working on writing some instrumentation code for LangChain, and wanted to verify it works with the
import-in-the-middle
hooks for ESM.I tried to do a bit of debugging to see what exactly was going on. Pointing the hook instead to my local version of
import-in-the-middle
, I tried to log thenamespace
we're re-exporting from. Doing that, I can see the namespace that's throwing the error is:Where it seems the problematic
RunTree
property is<uninitialized>
. I'm not entirely sure how this happens, or if it's a product of howimport-in-the-middle
is interacting with the transpiled code. The original file is defined here.Naive/noob question: would it be possible to just disable ESM parsing/loader hooks for certain files? This problematic file, from
langsmith
, is a sub-dependency oflangchain
, and is not a file I would want to instrument.I've tried changing my
hook.mjs
to be:Although this does not seem to be helpful, although it's likely I'm doing it wrong đ Also, my understanding is that will only work for newer versions of Node, and this integration I'm writing should be compatible back through Node 16. Any insights here are appreciated, happy to follow up with any additional information!