Search code examples
eslintecmascript-2016eslintrcecmascript-2017

Eslint not recognising destructing


My Eslint is not recognising that the below is valid code

const chai, { expect } = require('chai');

Can you please help me figure out which rule I need to add?


Solution

  • That's not an ESLint error, that's a pure syntax error. What you have in your example translates to:

    const chai;
    const { expect } = require('chai');
    

    As you can see a bit more clearly, you're essentially defining an uninitialized constant that can never be reassigned. Even the Node REPL will throw an error on that. Try the following snippet to see the error in action:

    const chai;

    If what you want is just the expect method from chai, then all you need is

    const { expect } = require('chai');
    

    If you need all of chai and expect an alternative is

    const chai = require('chai');
    const { expect } = chai;
    

    This would allow you to call expect(actual).to.be.an('object'); or chai.expect(actual).to.be.an('object');