1

I'm trying to execute code on the client side (not react, not node.js):

<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>
<script>
...
Transaction.from(Buffer.from(tx, 'base64'));
...
</script>

But i get error: Buffer is not defined

I'm trying:

<script src="https://cdn.jsdelivr.net/npm/@solana/web3.js@latest/lib/index.iife.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/buffer@latest/index.min.js"></script>
<script>
...
Transaction.from(Buffer.from(tx, 'base64'));
...
</script>

But i get error: Buffer.from is not a function.

How to solve this problem? Is it even possible to do this without node.js?

1 Answer 1

2

You don't "have to" use Node. I'm running a handful of dApps on Apache using bundled modules. But currently it is 100% a headache to do so.

What you have to do is use a tool like esbuild or browserify to bundle the NPM modules you need for the browser.

Please note it is doable but not optimal bundling web3 modules given you could need quite a few modules, and they update semi frequently. Bundling yourself will especially be an issue if you're not already running Node on your local development environment to run bundling tools.

Below is a link to all the individual bundled modules we use in one of our repos.

Example how to include the buffer.js bundle from the linked resource above into the window.

<script src="/path/to/your/buffer.js"></script>
<script>
  const Buffer = require("buffer").Buffer;
</script>
4
  • Unfortunately it's the correct answer. Additionally, if you're looking for vanilla js wallet connector option see here: link. At this current time I am one of the few devs building on Solana in non-node environments. If anyone has a better solution there are many devs who would love to hear it. Welcome to Web3.
    – Dapper
    Commented Jun 30 at 13:23
  • I haven't looked at your file, but the answer is generally correct. Oddly enough, you need to build the project via node.
    – Leo Loki
    Commented Aug 15 at 6:31
  • If you edit something in the answer, then I can change vote.
    – Leo Loki
    Commented Aug 15 at 6:31
  • 1
    I cleaned up the answer a bit and added a better resource for modules I bundled for Apache hosted Web3 apps.
    – Dapper
    Commented Aug 16 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.