When you're using the ES6 import syntax to bring in the web3
module, there's a hiccup once the typescript code gets compiled to javascript. The compilation results in a different syntax being used to import the web3
module, leading to the error of web3_1.default
being undefined
in the console.
If this issue is plaguing you, here are my suggestions:
- Switch to using the
require
syntax instead of import
.
const Web3 = require('web3');
const readonlyProvider = new Web3.providers.HttpProvider(providerURL);
- Turn on the
esModuleInterop
setting in your typescript configuration. This option alters how typescript manages imports, potentially resolving your problem.
{
"compilerOptions": {
"esModuleInterop": true
}
}
- Try out the
import * as
syntax. By importing all exports from the web3
module as object properties, typescript may handle things differently.
import * as Web3 from 'web3';
const readonlyProvider = new Web3.providers.HttpProvider(providerURL);
If these suggestions don't work for you or seem unhelpful, please let me know so I can remove them.