3

import { clusterApiUrl, Connection, Keypair, LAMPORTS_PER_SOL } from '/node_modules/@solana/web3.js';

The above line throwing the below error in my browser console. I am using plain html and javascript files to run this code.

Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.


That means I can't use simple html file to use any of solana blockchain end points.

May I know if Angular can be used?

2 Answers 2

2

the error you got back is descriptive and it means you are trying to load the package using .html file extension whilst this package can only be loaded on a javascript application or framework.

I suggest you start trying out Svelte, React and the rest.

5
  • Yes you can use Angular and the best way to get started would be the Solana Cookbook.. Solana CookBook Link Commented Dec 13, 2022 at 10:52
  • I have started implementaion on Angular but I am facing different issue now. I have installed Solana web3.js and just Import the web3.js at the top of my TS file. "node_modules/@solana/web3.js/lib/index.d.ts:6:30 - error TS7016: Could not find a declaration file for module 'node-fetch'. 'C:/Code/DEMO/FullStack UI/FullStack.UI/node_modules/node-fetch/lib/index.js' implicitly has an 'any' type. Try npm i --save-dev @types/node-fetch if it exists or add a new declaration (.d.ts) file containing declare module 'node-fetch'; 6 import * as nodeFetch from 'node-fetch';"
    – Rohit
    Commented Dec 16, 2022 at 14:58
  • okay so to resolve this you can run npm install node-fetch in your terminal and after doing that you can import node-fetch using import fetch from 'node-fetch'; .. this should resolve your issue...thanks Commented Dec 16, 2022 at 15:15
  • I have already tried but not succeded
    – Rohit
    Commented Dec 16, 2022 at 15:35
  • okay then why not try this npm i --save-dev @types/node-fetch then import the @solana/web3.js afterwards Commented Dec 16, 2022 at 15:39
2

You can import @solana/web3.js library into a plain HTML file via script tag.

<!-- Development (un-minified) -->
<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js"></script>

<!-- Production (minified) -->
<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.min.js"></script>

To connect to devnet cluster:

const connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl("devnet"));

Note: It is solanaWeb3 and not web3

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.