Import JS library via script tag
A JS SDK library I need to use recommends importing a browser bundle by adding a script tag to index.html:
Can I add this to a route index.tsx for the page where I will use this SDK? And then, how do I import it and use functions from the SDK?
22 Replies
hmmm, since it is served in unpkg, why not install the npm package
npm
algosdk
The official JavaScript SDK for Algorand. Latest version: 1.23.2, last published: 22 days ago. Start using algosdk in your project by running
npm i algosdk
. There are 154 other projects in the npm registry using algosdk.I'm getting an error that I'm totally stumped how to solve
well you can add that script
Dev mode works fine, but trying to do a build fails
'default' is not exported by node_modules/.pnpm/[email protected]/node_modules/js-sha512/src/sha512.js, imported by node_modules/.pnpm/[email protected]/node_modules/algosdk/dist/esm/src/nacl/naclWrappers.js
file: /Users/name/Code/alright-solid/node_modules/.pnpm/[email protected]/node_modules/algosdk/dist/esm/src/nacl/naclWrappers.js:2:7
1: import nacl from 'tweetnacl';
2: import sha512 from 'js-sha512';
^
3: export function genericHash(arr) {
4: return sha512.sha512_256.array(arr);
btw are you using Vite or SolidStart
Both?
so SolidStart then
yes
you can add that script to root.tsx
I've been going down a rabbit hole of trying to fix this build error
of course, your last problem would be how to use it with TS
in which I cannot help you
because one of the dependencies of algosdk use require()
Can I have this script only load when on a certain page route, though?
That's why I was thinking of putting it in a particular route's index.tsx
Any idea where I could see an example of a project that relies on a library that is imported via <script>?
not in SPA. If you visit page A as initial page then went to page B using the routing, the script is still loaded
I want this library only to be loaded on page B
but not on landing page A
well yeah what I'm saying is
once you change pages via router, the script is still loaded
unlike in MPAs
say if you go
page A: no script loaded
page B: script is loaded
back to page A: script is loaded (due to page B)
but that's kind of secondary to my main issue which is I have no idea how to use this library and cant get a production build to work
this is fine
I just don't want this SDK library to load on the marketing page lol
only when they get deeper into the app
Whoa, thank you
Let me try this
@mdynnl I used solid.new to create a clean example to try this
probably forgot to install it
ok that fixed that error
trying in my local project now
So I can get the build script to work
but it fails on starting the build
This is the crux of my issue
that require() is in some of the libraries that are used internally by algosdk
server-*.js has this section:
which seems to be what breaks the ES module
But I don't know how to fix this