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

Voyager is using NodeJS modules which are problematic browser bundles #75

Open
silkroadnomad opened this issue Mar 12, 2025 · 2 comments · May be fixed by #85
Open

Voyager is using NodeJS modules which are problematic browser bundles #75

silkroadnomad opened this issue Mar 12, 2025 · 2 comments · May be fixed by #85

Comments

@silkroadnomad
Copy link
Contributor

silkroadnomad commented Mar 12, 2025

When importing voyager into a browser app in order to add an orbitdb to voyager, it is necessary to use polyfills in order to suppress compilation and bundling errors.

It would be easier if that library works out of the box for browser and NodeJS without configuring polyfills.
Not sure if that is possible by providing a separate build for browsers.

@silkroadnomad
Copy link
Contributor Author

silkroadnomad commented Mar 13, 2025

Node.js Module Resolution Fix for Browser Environment

Problem

The application was encountering the following error during runtime:

Uncaught TypeError: Failed to resolve module specifier "path". Relative references must start with either "/", "./", or "../".

This occurred because Node.js built-in modules (particularly path) were being directly imported in browser environments where they aren't natively available.

Solution

The issue was resolved by implementing a comprehensive polyfill strategy in the Vite configuration:

  1. Properly configured vite-plugin-node-polyfills with protocolImports: true
  2. Added explicit aliases for all Node.js built-ins used in the application
  3. Pre-bundled polyfill packages to ensure they're properly transformed
  4. Set appropriate global definitions to maintain compatibility

Implementation Details

// In vite.config.ts
resolve: {
      alias: {
            // Explicit aliasing for Node.js built-ins
            path: 'path-browserify',
            'node:path': 'path-browserify',
      }
},
optimizeDeps: {
      esbuildOptions: {
          define: {
              global: 'globalThis',
          },
          format: 'esm',
      },
      include: ['path-browserify']
}

npm install path-browserify --save-dev

@silkroadnomad
Copy link
Contributor Author

It turned out those changes aren't enough and I did another change on the voyager side.

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

Successfully merging a pull request may close this issue.

1 participant