'Error: Uncaught TypeError: Failed to resolve module specifier "web3". Relative references must start with either "/", "./", or "../"
I have installed web3 using npm in my project. I now have a web3 folder in my node_modules folder. However, when I run my code on local server, it shows the following error:
Uncaught TypeError: Failed to resolve module specifier "web3". Relative references must start with either "/", "./", or "../". (network.html:1
In my networks.js file, I have done "import { Web3 } from 'web3';" and in my network.html file, inside script, I have added
type="module" src="/js/networks.js"
and
src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"
I tried to write the import statement in my .js file as a relative location i.e. ./web3 , /web3, ../web3. However, it still shows the same error.
Solution 1:[1]
Since you are directly loading the Web3 library using CDN in HTML then you don't need to install the module using NPM. Web3 module exposes value of Web3 in global context which means, you can directly start using the Web3 in network.js file after Web3 script is loaded.
Refer: https://stackblitz.com/edit/web-platform-fclq8f?file=js/network.js
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Pavan |