$ npm init
$ npm install --save-dev @nomiclabs/buidler
$ npm install --save-dev @nomiclabs/buidler-ethers ethers
$ npm install --save-dev ts-node typescript
$ npm install --save-dev chai @types/node @types/mocha @types/chai
[create buidler.config.ts]
[create tsconfig.json]
[create scripts/Example1.ts]
[create scripts/Example2.ts]
import { usePlugin } from "@nomiclabs/buidler/config"
usePlugin("@nomiclabs/buidler-ethers")
export default {}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"outDir": "dist"
},
"include": ["./scripts", "./test"],
"files": [
"./buidler.config.ts",
"./node_modules/@nomiclabs/buidler-ethers/src/type-extensions.d.ts"
]
}
Given the following script:
import { ethers } from '@nomiclabs/buidler'
async function example (provider: ethers.providers.Provider) {
let blockNumber = await provider.getBlockNumber()
console.log(`Block number: ${blockNumber}`)
}
example(ethers.getDefaultProvider()).catch(console.error)
Attempting to run it fails during TypeScript compilation:
$ npx buidler run scripts/Example1.ts
TSError: Unable to compile TypeScript:
scripts/Example1.ts:3:35 - error TS2503: Cannot find namespace 'ethers'.
3 async function example (provider: ethers.providers.Provider) {
Given the following script:
import { ethers } from 'ethers'
async function example (provider: ethers.providers.Provider) {
let blockNumber = await provider.getBlockNumber()
console.log(`Block number: ${blockNumber}`)
}
example(ethers.getDefaultProvider()).catch(console.error)
This example compiles and runs as expected.
$ npx buidler run scripts/Example2.ts
Block number: 9833062