Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SDK in browser env (Vite) #1314

Open
kelreel opened this issue Nov 9, 2024 · 0 comments
Open

Using SDK in browser env (Vite) #1314

kelreel opened this issue Nov 9, 2024 · 0 comments

Comments

@kelreel
Copy link

kelreel commented Nov 9, 2024

I can't use sdk in a browser environment with React & Vite.

You can also try to try to make app from vite-react-typescript template and catch the following errors:

{
    "dependencies": {
      "@drift-labs/sdk": "^2.100.0-beta.8",
      "@solana/web3.js": "^1.95.4",
      "bs58": "^6.0.0",
      "react": "^18.3.1",
      "react-dom": "^18.3.1",
      "rpc-websockets": "7.10.0"
    },
    "devDependencies": {
      "@eslint/js": "^9.13.0",
      "@types/react": "^18.3.12",
      "@types/react-dom": "^18.3.1",
      "@vitejs/plugin-react": "^4.3.3",
      "eslint": "^9.13.0",
      "eslint-plugin-react-hooks": "^5.0.0",
      "eslint-plugin-react-refresh": "^0.4.14",
      "globals": "^15.11.0",
      "typescript": "~5.6.2",
      "typescript-eslint": "^8.11.0",
      "vite": "^5.4.10"
    }
}
// vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vite.dev/config/
export default defineConfig({
  define: {
    "process.env": {}, // fix "process not defined"
  },
  plugins: [react()],
  // optimizeDeps: {
  //   exclude: ["@drift-labs/sdk"],
  // },
});
// App.tsx

  const connection = new Connection("https://api.mainnet-beta.solana.com");

  const keypair = Keypair.fromSecretKey(
    bs58.decode(
      "secret key"
    )
  );

  console.log(keypair);
  const wallet = keypair ? new Wallet(keypair) : null;

  console.log(wallet);

The following error occurs here:
image
image

With excluded Vite optimized deps:
image

Seems like Node dependencies still exists in sdk browser version.
I also plan to use the DriftClient in the browser, do you think it will work? 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant