1

I would like to import specific properties from lodash and export them under the same object.

Right now I am doing

import { omit, get, set } from 'lodash';

const _ = {
  omit,
  get,
  set
};

export default _;

As you can see, I need to repeat each and every property twice.

Is there some syntax that allows me to do it once? for example something like:

import { 
  omit, 
  get, 
  set } as _ from 'lodash';

export default _;

would be great! but this is obviously not supported at the moment.

Is there any other syntax that allows this?

3
  • 2
    Did you try export { omit, get, set } from 'lodash';? Commented Sep 24, 2019 at 14:25
  • No.. was not aware this is an option. can you please post this an answer so I can approve? Commented Sep 24, 2019 at 14:36
  • @HarunYilmaz this actually breaks my code - i need to change all my imports to accommodate to your suggestion. import * as _ from .. any way around it? Commented Sep 24, 2019 at 14:41

1 Answer 1

3

You can export directly from a module as following:

export { omit, get, set } from 'lodash';

For further reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

Sign up to request clarification or add additional context in comments.

4 Comments

this would require to change related imports to be import * as _ from .... any way around it?
I couldn't totally understand your intention. Can you please share how do you want to import these exported variables?
As a workaround to import * as... change, I did the following - I converted the file to folder with 2 files lodash_breakdown and index. in lodash_breakdown I do what you suggest. in index file I do the import * as _ from './lodash_breakdown' and export is default export default _;. This helped me avoid going over all existing imports.
I'd appreciate it if you could incorporate this in your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.