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

error "cannot read properties of undefined (reading 'prototype')" when use umi bundle in vite (nuxt 3) #147

Open
viandwi24 opened this issue Sep 3, 2024 · 3 comments

Comments

@viandwi24
Copy link

this error :

Cannot read properties of undefined (reading 'prototype')
at http://localhost:3000/_nuxt/node_modules/.cache/vite/client/deps/@metaplex-foundation_umi-bundle-defaults.js?v=eaf4a82e:2834:78

but when im using this minimal setup (directly from @metaplex-foundation/umi) is working

import { createUmi, type RpcInterface, type Umi } from '@metaplex-foundation/umi'
import { mplTokenMetadata } from '@metaplex-foundation/mpl-token-metadata'
import { mplBubblegum } from '@metaplex-foundation/mpl-bubblegum'
import { dasApi, type DasApiInterface } from "@metaplex-foundation/digital-asset-standard-api"
import { web3JsRpc } from '@metaplex-foundation/umi-rpc-web3js'
import { defaultProgramRepository } from '@metaplex-foundation/umi-program-repository'

const umi = createUmi()
      .use(defaultProgramRepository())
      .use(web3JsRpc(this.options.rpc))
      .use(mplTokenMetadata())
      .use(mplBubblegum())
      .use(dasApi())

my nuxt config :

export default defineNuxtConfig({
  ssr: false,
  vite: {
    esbuild: {
      target: "esnext",
    },
    build: {
      target: "esnext",
    },
    optimizeDeps: {
      include: [
        "@solana/web3.js",
        "@coral-xyz/anchor",
        "@metaplex-foundation/umi",
        "@metaplex-foundation/umi-bundle-defaults",
        "@metaplex-foundation/mpl-token-metadata",
        "@metaplex-foundation/digital-asset-standard-api",
        "@metaplex-foundation/mpl-bubblegum",
        "@metaplex-foundation/umi-rpc-web3js",
        "@metaplex-foundation/umi-program-repository",
        "buffer"
      ],
      esbuildOptions: {
        target: "esnext",
      },
    },
    define: {
      "process.env.BROWSER": true,
      "globalThis": "window",
      "global": "window",
    },
  },
}
@kevinrodriguez-io
Copy link

Same here with react + vite.

@kevinrodriguez-io
Copy link

I managed to solve all my issues with this config:

import react from '@vitejs/plugin-react-swc';
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import topLevelAwait from 'vite-plugin-top-level-await';
import wasm from 'vite-plugin-wasm';

import path from 'path';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    react(),
    wasm(), // Ignore, this is for my project
    topLevelAwait(), // This is for my project too
    nodePolyfills({
      // Whether to polyfill specific Node.js modules in the browser
      protocolImports: true,
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'), // Not needed
      stream: 'stream-browserify', // This fixes the prototype thing
    },
  },
  define: {
    global: 'globalThis', // This fixes an issue with globals and stuff
  },
});

@kevinrodriguez-io
Copy link

Still, it doesn't seem normal that you have to polyfill node streams on the browser.

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

2 participants