I am using ethers version 6.2.3, You can directly import it from ethers package.
const { ethers, JsonRpcProvider } = require('ethers');
For more info refer to this link
https://github.com/ethers-io/ethers.js/issues/3924.
Entire code I am using to deploy contract locally using ethers on Ganache
//importing thers and JsonRpcProvider
const { ethers, JsonRpcProvider } = require('ethers');
const fs = require('fs-extra');
async function main() {
// Json Rpc Provider - Connecting to local blockchain
const provider = new JsonRpcProvider('address'); // add your rpc server url from Ganache
// Connect to wallet to sign transactions
const wallet = new ethers.Wallet(
'Add your private key here',
provider
);
//Read ABI which you get post compilation using solc
const abi = fs.readFileSync('./fileName.abi', 'utf8');
//Read Binary which you get post compilation using solc
const binary = fs.readFileSync(
'./fileName.bin',
'utf8'
);
//Create Contract factory object to deploy
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log('Deploying, Please wait...');
// Deploy and you are good to go
const contract = await contractFactory.deploy();
console.log(contract);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Response
Deploying, Please wait...
BaseContract {
target: '0x35aD58985b61f634A8108A7ffcF6D438ab701ffB',
interface: Interface {
fragments: [ [FunctionFragment], [FunctionFragment], [FunctionFragment] ],
deploy: ConstructorFragment {
type: 'constructor',
inputs: [],
payable: false,
gas: null
},
fallback: null,
receive: false
},
runner: Wallet {
provider: JsonRpcProvider {},
address: '0x8EDA89abFAB838dc350c12fA8b6D9Ad630380B2f'
},
filters: {},
fallback: null,
[Symbol(_ethersInternal_contract)]: {}
}