Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 253
dagda1 Avatar asked Jan 12 '17 07:01

dagda1


2 Answers

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');
like image 50
jc-alvaradov Avatar answered Oct 07 '22 08:10

jc-alvaradov


You can pick parts of lodash to require, there are example on the lodash docs page

e.g.

const at = require('lodash/at');
like image 45
bsyk Avatar answered Oct 07 '22 08:10

bsyk