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).
plugins: [vue(), nodePolyfills()]
– PostdoctoralBuffer
twice, there's currently an outstanding issue with Vite github.com/vitejs/vite/issues/7384 – Peria