importecmascript-6

ES6 shorthand import


Is there a shorter ES6 way of doing:

const assert = require('chai').assert;

than

import chai from 'chai';
const assert = chai.assert;

(chai is the chai-assertion library, in case you haven't heard of it yet.)


Solution

  • Yes, you can do it like:

    import { assert } from 'chai';
    

    assert must be exporting from chai in that case. See spec here and about es6 modules here