Dynamic imports in LangChain document loaders causes lots of problems in different environments #7469
Open
5 tasks done
Labels
auto:bug
Related to a bug, vulnerability, unexpected error with an existing feature
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
No response
Description
I am trying to use the PDFLoader class to load a PDF document from a jest testing environment where testEnvironment is set to "node" in jest.config.js file. When running the test I get the error:
Error: Failed to load fs/promises. TextLoader available only on environment 'node'. It appears you are running environment 'node'
The error is due to using dynamic import in BufferLoader class and is misleading because the environment is correctly set to node.
You can get past the above error by passing the option --experimental-vm-modules to node. But then you get another error:
Failed to load pdf-parse. Please install it with eg. 'npm install pdf-parse'
even though pdf-parse is already installed. The second error in PDFLoader is also due to dynamic import but then for importing a very specific version of pdf-parse which I did not have. Again the error message is misleading.
IMO, dynamic imports should be removed from LangChain as it causes problems in different environments. Instead, just import parsers like pdf-parse normally from PDFLoader. The document loaders are useless with this types of problems. Just look at the number of problems people are having due to this issue.
System Info
Platform: windows
node version: 22.12.0
npm version: 10.9.0
Langchain versions:
"@langchain/community": "^0.3.22",
"@langchain/core": "^0.3.27",
"@langchain/openai": "^0.3.16",
"langchain": "^0.3.9",
The text was updated successfully, but these errors were encountered: