Search code examples
node.jscommonjs

only require specific exports from a required commonjs module


In es6 I can do this:

const { findIndex, last } from 'lodash';

Can I do the same in commonjs with require?


Solution

  • Yes, you can use destructuring in the same way in node v6 and superior. More info here: Destructuring in Node.JS

    Example:

    const { findIndex, last } = require('lodash');