简体   繁体   中英

How to disable eslint 'no-undef' for a specific package import?

I'm working with a framework ('hardhat') that automatically requires a package ('ethers') but eslint keeps calling it out as undefined. Requiring 'ethers' is not a solution because it just breaks everything; but from what I understand, a way to add exceptions to the entire document is to place overrides in a comment block above the entire thing.

 /*
    eslint-disable jest/valid-expect
 */ 
const { expect } = require("chai");
const { txHist } = require("../scripts/utils.js");

describe("DStor", () => {
    let DStor;
    let deployer, user1, user2, user3, users; // eslint-disable-line no-unused-vars

    beforeEach(async () => {
        // Get ContractFactory and Signers
        const DStorFactory = await ethers.getContractFactory("DStor"); // 'ethers' is highlighted with no-undef
...

I suspect the solution is to add another eslint-disable rule, but I don't know how to target the 'ethers' package with it. Anyone have any solutions?

Update: fixed by adding const { ethers } = require("hardhat");

Have a look at ESLint globals . Basically, you can declare a global value ethers in your ESLint configuration, and that will be treated as defined in all linted files.

For ESLint >= 7, if your configuration file is.eslintrc, add an entry like:

{
    "globals": {
        "ethers": "readonly"
    }
}

For ESLint < 7, use "ethers": false instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM