I'm working on a Vite project that uses the opensea-js
package. This package depends on xhr2-cookies
, which imports os
, http
, https
and others Some internal node modules.
When I try to call any opensea method, I get the following error:
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
Trace this error and find that it comes from building the user agent string.
I tried installing rollup-plugin-polyfill-node
and adding it to vite.config.js
but I still get the same error: < ;/p>
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 also tried manually repairing the file using patch-package
which resolved the os
error, but it failed when trying to send the request (because it uses polyfillhttp
/https
module).
I used
rollup-plugin-polyfill-node
to solve this problem.Here is a more complete solution based on Fabiano's answer:
In my project, I used the following configuration to solve the problem. I describe the solution in ashort article.