OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

I tried to import ccxt into my svelte kit app and got ReferenceError: Buffer is not defined even though I'd only imported it

  • Thread starter Thread starter plutownium
  • Start date Start date
P

plutownium

Guest
I have a minimal reproducible example here about this issue. The issue is that import ccxt from "ccxt" gives this error

Code:
ReferenceError: Buffer is not defined
    at node_modules/ccxt/js/static_dependencies/node-rsa/schemes/pkcs1.js (pkcs1.js:10:10)
    at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
    at node_modules/ccxt/js/static_dependencies/node-rsa/schemes/schemes.js (schemes.js:2:12)
    at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
    at node_modules/ccxt/js/static_dependencies/node-rsa/libs/rsa.js (rsa.js:12:15)
    at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
    at node_modules/ccxt/js/static_dependencies/node-rsa/NodeRSA.js (NodeRSA.js:8:11)
    at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
    at node_modules/ccxt/js/base/functions/crypto.js (crypto.js:8:17)
    at __require (chunk-RSJERJUL.js?v=d0062138:3:50)

steps to reproduce:

  1. use the create a project steps
  2. npm install ccxt
  3. in the /src/routes/+page.svelte use import ccxt from "ccxt"

My expected behavior is that the package imports and I can do like what the docs say to do

Code:
var ccxt = require ('ccxt')

console.log (ccxt.exchanges) // print all available exchanges

i saw this post say basically "to get require to work, try adding it as a dev dependency" so i did that:

npm install --save-dev ccxt

require still fails. and anyway I don't want to use require, I want to use "import".

edit:

I managed to use Uncaught ReferenceError: Buffer is not defined to make my vite.config.js do this

Code:
import path from 'path'
import inject from '@rollup/plugin-inject'

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
    plugins: [sveltekit()],
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src'),
        }
    },
    build: {
        rollupOptions: {
            plugins: [inject({ Buffer: ['buffer', 'Buffer'] })],
        },
    },
})

and now i can console.log(ccxt.exchanges) server side, but client side it still errors. but, progress.

edit2: I also tried the ticked answer to ReferenceError: Buffer is not defined in vite/sveltekit with Torus which says

Code:
resolve: {
                alias: {
                    '@toruslabs/openlogin': path.resolve(
                        './node_modules/@toruslabs/openlogin/dist/openlogin.umd.min.js'
                    )
                }
            }

but this fails as well.

I tried the answer by "Goutham J.M" with many upvotes that was long and complex, and got stuck with error [ERR_MODULE_NOT_FOUND]: Cannot find package '@esbuild-plugins/node-globals-polyfill' but I don't know how to solve that and it's too much of a rabbit hole.

<p>I have a minimal reproducible example here about this issue. The issue is that <code>import ccxt from "ccxt"</code> gives this error</p>
<pre><code>ReferenceError: Buffer is not defined
at node_modules/ccxt/js/static_dependencies/node-rsa/schemes/pkcs1.js (pkcs1.js:10:10)
at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
at node_modules/ccxt/js/static_dependencies/node-rsa/schemes/schemes.js (schemes.js:2:12)
at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
at node_modules/ccxt/js/static_dependencies/node-rsa/libs/rsa.js (rsa.js:12:15)
at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
at node_modules/ccxt/js/static_dependencies/node-rsa/NodeRSA.js (NodeRSA.js:8:11)
at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
at node_modules/ccxt/js/base/functions/crypto.js (crypto.js:8:17)
at __require (chunk-RSJERJUL.js?v=d0062138:3:50)
</code></pre>
<p>steps to reproduce:</p>
<ol>
<li>use the <a href="https://kit.svelte.dev/docs/creating-a-project" rel="nofollow noreferrer">create a project steps</a></li>
<li><code>npm install ccxt</code></li>
<li>in the <code>/src/routes/+page.svelte</code> use <code>import ccxt from "ccxt"</code></li>
</ol>
<p>My expected behavior is that the package imports and I can do like <a href="https://github.com/ccxt/ccxt" rel="nofollow noreferrer">what the docs say to do</a></p>
<pre><code>var ccxt = require ('ccxt')

console.log (ccxt.exchanges) // print all available exchanges
</code></pre>
<p>i saw <a href="https://stackoverflow.com/questions/69369543/require-is-not-defined-sveltekit-with-typescript">this post</a> say basically "to get <code>require</code> to work, try adding it as a dev dependency" so i did that:</p>
<p><code>npm install --save-dev ccxt</code></p>
<p>require still fails. and anyway I don't want to use require, I want to use "import".</p>
<p>edit:</p>
<p>I managed to use <a href="https://github.com/nuxt/nuxt/issues/14202" rel="nofollow noreferrer">Uncaught ReferenceError: Buffer is not defined</a> to make my vite.config.js do this</p>
<pre><code>import path from 'path'
import inject from '@rollup/plugin-inject'

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [sveltekit()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
}
},
build: {
rollupOptions: {
plugins: [inject({ Buffer: ['buffer', 'Buffer'] })],
},
},
})
</code></pre>
<p>and now i can <code>console.log(ccxt.exchanges)</code> server side, but client side it still errors. but, progress.</p>
<p>edit2: I also tried the ticked answer to <a href="https://stackoverflow.com/questions...or-buffer-is-not-defined-in-vite-sveltekit-wi">ReferenceError: Buffer is not defined in vite/sveltekit with Torus</a> which says</p>
<pre><code>resolve: {
alias: {
'@toruslabs/openlogin': path.resolve(
'./node_modules/@toruslabs/openlogin/dist/openlogin.umd.min.js'
)
}
}
</code></pre>
<p>but this fails as well.</p>
<p>I tried the answer by "Goutham J.M" with many upvotes that was long and complex, and got stuck with error <code>[ERR_MODULE_NOT_FOUND]: Cannot find package '@esbuild-plugins/node-globals-polyfill'</code> but I don't know how to solve that and it's too much of a rabbit hole.</p>
 

Latest posts

Top