15

I'm working on a Vite project which uses the opensea-js package. This package depends on xhr2-cookies. which imports os, http, https and some other internal node modules.

I'm getting this error when trying to call any of the opensea methods:

Uncaught (in promise) TypeError: os.type is not a function
    XMLHttpRequest2 xml-http-request.ts:102
    prepareRequest httpprovider.js:61
    sendAsync httpprovider.js:116
    node_modules opensea-js.js:24209

Tracing this error it comes from constructing the useragent string.

I tried installing rollup-plugin-polyfill-node and adding it to vite.config.js but still getting the same error:

import path from 'path'
import vue from '@vitejs/plugin-vue'
import nodePolyfills from 'rollup-plugin-polyfill-node'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
    },
  },
  server: {
    port: 8080,
  },
  define: {
    'process.env': {},
  },
  build: {
    rollupOptions: {
      plugins: [
        nodePolyfills(),
      ],
    },
  },
})

I've also tried patching the file manually with patch-package, which fixes the os error however then fails when trying to sending the request (which uses http/https modules which also need to be polyfilled).

Benedict Lewis
  • 2,733
  • 7
  • 37
  • 78
  • maybe you should add the polyfills to your plugins like `plugins: [vue(), nodePolyfills()]` – mecograph Nov 22 '21 at 00:00
  • Note - if these solutions end up importing `Buffer` twice, **there's currently an outstanding issue with Vite** https://github.com/vitejs/vite/issues/7384 – mikemaccana Aug 22 '22 at 17:32

4 Answers4

19

In my project I solved with a configuration like this. I have described the solution in a short article.

// yarn add --dev @esbuild-plugins/node-globals-polyfill
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
// yarn add --dev @esbuild-plugins/node-modules-polyfill
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
// You don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfill
import rollupNodePolyFill from 'rollup-plugin-node-polyfills'

export default {
        resolve: {
            alias: {
                // This Rollup aliases are extracted from @esbuild-plugins/node-modules-polyfill, 
                // see https://github.com/remorses/esbuild-plugins/blob/master/node-modules-polyfill/src/polyfills.ts
                // process and buffer are excluded because already managed
                // by node-globals-polyfill
                util: 'rollup-plugin-node-polyfills/polyfills/util',
                sys: 'util',
                events: 'rollup-plugin-node-polyfills/polyfills/events',
                stream: 'rollup-plugin-node-polyfills/polyfills/stream',
                path: 'rollup-plugin-node-polyfills/polyfills/path',
                querystring: 'rollup-plugin-node-polyfills/polyfills/qs',
                punycode: 'rollup-plugin-node-polyfills/polyfills/punycode',
                url: 'rollup-plugin-node-polyfills/polyfills/url',
                string_decoder:
                    'rollup-plugin-node-polyfills/polyfills/string-decoder',
                http: 'rollup-plugin-node-polyfills/polyfills/http',
                https: 'rollup-plugin-node-polyfills/polyfills/http',
                os: 'rollup-plugin-node-polyfills/polyfills/os',
                assert: 'rollup-plugin-node-polyfills/polyfills/assert',
                constants: 'rollup-plugin-node-polyfills/polyfills/constants',
                _stream_duplex:
                    'rollup-plugin-node-polyfills/polyfills/readable-stream/duplex',
                _stream_passthrough:
                    'rollup-plugin-node-polyfills/polyfills/readable-stream/passthrough',
                _stream_readable:
                    'rollup-plugin-node-polyfills/polyfills/readable-stream/readable',
                _stream_writable:
                    'rollup-plugin-node-polyfills/polyfills/readable-stream/writable',
                _stream_transform:
                    'rollup-plugin-node-polyfills/polyfills/readable-stream/transform',
                timers: 'rollup-plugin-node-polyfills/polyfills/timers',
                console: 'rollup-plugin-node-polyfills/polyfills/console',
                vm: 'rollup-plugin-node-polyfills/polyfills/vm',
                zlib: 'rollup-plugin-node-polyfills/polyfills/zlib',
                tty: 'rollup-plugin-node-polyfills/polyfills/tty',
                domain: 'rollup-plugin-node-polyfills/polyfills/domain'
            }
        },
        optimizeDeps: {
            esbuildOptions: {
                // Node.js global to browser globalThis
                define: {
                    global: 'globalThis'
                },
                // Enable esbuild polyfill plugins
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true
                    }),
                    NodeModulesPolyfillPlugin()
                ]
            }
        },
        build: {
            rollupOptions: {
                plugins: [
                    // Enable rollup polyfills plugin
                    // used during production bundling
                    rollupNodePolyFill()
                ]
            }
        }
}
Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49
  • 1
    I had to add `buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6',` to the `alias` object in order to get my build to compile for production. – JiFus Jan 26 '22 at 15:36
  • 1
    @JiFus in my project adding it breaks the build, need to investigate better. – Fabiano Taioli Jan 26 '22 at 15:44
  • @FabianoTaioli Did you find out why? – cascading-jox May 27 '22 at 04:02
  • 3
    indeed, the `buffer` alias is needed for me too, and the `process` was also needed for me: `buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6', process: 'rollup-plugin-node-polyfills/polyfills/process-es6'` – Tbaut Jul 18 '22 at 12:32
  • 3
    It looks like there's a dir missing in your `alias` setup. `Could not read from file: /home/mike/Code/myproject/rollup-plugin-node-polyfills/polyfills/string-decoder` – mikemaccana Aug 23 '22 at 10:24
  • Didn't work for me, I got: `No matching export in "node-modules-polyfills:util" for import "types"` – griest Oct 30 '22 at 20:08
  • any idea why am I getting the following `@esbuild-plugins/node-globals-polyfill/_buffer.js" cannot be marked as external`? – Terchila Marian Jan 24 '23 at 09:45
  • Looks like esbuild bug, see: https://github.com/remorses/esbuild-plugins/issues/27#issuecomment-1403271447 – Sam Sussman Jan 25 '23 at 22:42
5

I used rollup-plugin-polyfill-node to fix the issue.

import nodePolyfills from 'rollup-plugin-polyfill-node';
rollup({
  entry: 'main.js',
  plugins: [
    nodePolyfills( /* options */ )
  ]
})

Here is a more complete answer based on Fabiano's answer:

// yarn add --dev @esbuild-plugins/node-globals-polyfill
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
// yarn add --dev @esbuild-plugins/node-modules-polyfill
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill'
import nodePolyfills from 'rollup-plugin-polyfill-node';

export default {
        optimizeDeps: {
            esbuildOptions: {
                // Node.js global to browser globalThis
                define: {
                    global: 'globalThis'
                },
                // Enable esbuild polyfill plugins
                plugins: [
                    NodeGlobalsPolyfillPlugin({
                        process: true,
                        buffer: true
                    }),
                    NodeModulesPolyfillPlugin()
                ]
            }
        },
        build: {
            rollupOptions: {
                plugins: [
                    // Enable rollup polyfills plugin
                    // used during production bundling
                    nodePolyfills()
                ]
            }
        }
}
mohghaderi
  • 2,520
  • 1
  • 19
  • 12
2

Adding my own answer, as the ones above caused issues with Buffer being loaded twice, and seemed to have issues regarding paths.

Borrowed from Metaplex JS examples for Vite:

// https://vitejs.dev/config/
import { defineConfig } from "vite";
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import nodePolyfills from "rollup-plugin-node-polyfills";
import { svelte } from "@sveltejs/vite-plugin-svelte";

// Config is based on metaplex + vite example from:
// https://github.com/metaplex-foundation/js-examples/tree/main/getting-started-vite

// es2020 Needed for BigNumbers
// See https://github.com/sveltejs/kit/issues/859

export default defineConfig({
  plugins: [svelte()],
  resolve: {
    alias: {
      stream: "rollup-plugin-node-polyfills/polyfills/stream",
      events: "rollup-plugin-node-polyfills/polyfills/events",
      assert: "assert",
      crypto: "crypto-browserify",
      util: "util",
    },
  },
  define: {
    "process.env": process.env ?? {},
  },
  build: {
    target: "es2020",
    rollupOptions: {
      plugins: [nodePolyfills({ crypto: true })],
    },
  },
  optimizeDeps: {
    esbuildOptions: {
      plugins: [NodeGlobalsPolyfillPlugin({ buffer: true })],
      target: "es2020",
    },
  },
});

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
2

I used Fabiano's answer before, but got an error NodeGlobalPolyfillPlugin() when build the project. After stumbling around I found this package that was quite easy to use and worked well with my case.

This is the link: https://www.npmjs.com/package/vite-plugin-ngmi-polyfill

import { defineConfig } from "vite";
import { NgmiPolyfill } from "vite-plugin-ngmi-polyfill";

export default defineConfig({
  plugins: [NgmiPolyfill()],
});