langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

Import error when using in React-vite project

Open KtechB opened this issue 1 year ago • 3 comments

First of all, I'm glad to use langchain in Javascript. Thank you to the creators.

However when I tried to use langchain.js in React-vite project, the code seems to fail.

This code is React-vite initial code with langchain's LLM import.

import { useState } from "react";
import reactLogo from "./assets/react.svg";
import "./App.css";
import { OpenAI } from "langchain";
function App() {
  const [count, setCount] = useState(0);
  const llm = new OpenAI({ temperature: 0.9 });

  return (
    <div className="App">
      <div>
        <a href="https://vitejs.dev" target="_blank">
          <img src="/vite.svg" className="logo" alt="Vite logo" />
        </a>
        <a href="https://reactjs.org" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </div>
  );
}

export default App;

Run this app yarn dev then, I got this error in console.

Uncaught SyntaxError: The requested module '/node_modules/.vite/deps/langchain.js?v=74bcca3d' does not provide an export named 'OpenAI' (at App.tsx:4:10)

Not only OpenAI, other classes also give me import error.

Running node v16.13.0 on macOs Montery 12.3.1 , apple silicon m1 chip.

Anyone know how to resolve this probrem?

KtechB avatar Feb 21 '23 15:02 KtechB