Polyfill node os module with vite/rollup.js
Asked Answered
M

5

16

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).

Merrillmerrily answered 22/9, 2021 at 14:34 Comment(2)
maybe you should add the polyfills to your plugins like plugins: [vue(), nodePolyfills()]Postdoctoral
Note - if these solutions end up importing Buffer twice, there's currently an outstanding issue with Vite github.com/vitejs/vite/issues/7384Peria
L
20

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()
                ]
            }
        }
}
Label answered 11/1, 2022 at 11:28 Comment(9)
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.Berberidaceous
@Berberidaceous in my project adding it breaks the build, need to investigate better.Label
@FabianoTaioli Did you find out why?Assamese
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'Tolle
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-decoderPeria
Didn't work for me, I got: No matching export in "node-modules-polyfills:util" for import "types"Overstock
any idea why am I getting the following @esbuild-plugins/node-globals-polyfill/_buffer.js" cannot be marked as external?Plasticizer
Looks like esbuild bug, see: github.com/remorses/esbuild-plugins/issues/…Ceruse
This actually worked for me. I also had to add the buffer property as indicated by @jifusDubose
J
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()
                ]
            }
        }
}
Jin answered 30/5, 2022 at 23:16 Comment(0)
G
5

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()],
});
Glorygloryofthesnow answered 23/11, 2022 at 15:46 Comment(2)
This was probably the easier config, and it also worked for me!Norven
This should be the best answer with the latest Vite version (5 and mjs configuration). ThanksMazza
P
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",
    },
  },
});

Peria answered 23/8, 2022 at 15:53 Comment(0)
O
0

Add the following to your vite config to stop vite/rollup from trying to bundle node dependencies. No polyfill plugin required.

import { builtinModules } from 'module';

const allExternal = [
    ...builtinModules,
    ...builtinModules.map((m) => `node:${m}`)
]

// add the following to your config object

return {
    build: {
            rollupOptions: {
                external: ...allExternal
            }
    }
}

Outwear answered 10/7 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.