2

Let's say I have multiple javascript files, along with several node modules, and in each file I do something like this:

import 'node_module_a'
import 'node_module_b'
import 'node_module_c'

I find myself repeating this same code across all my javascript files, so I'm wondering is there a compact way (like bundling) to add all this to a single file, and just import from that?

Like:

in all_the_things.js:

import 'node_module_a'
import 'node_module_b'
import 'node_module_c'

Then add only this line in all javascript files:

import * from 'all_the_things.js'

I tried reading some documentation to give me ideas, but I'm at a loss here.

1
  • Yes, just create your own module.. with those lines in, and instead import that one..
    – Keith
    Commented Apr 5, 2018 at 23:05

1 Answer 1

1

I had no idea I literally wrote the answer in the question. I tried all kinds of stuff, except what I posted. When I tried it, it worked.

If you put

in all_the_things.js:

import 'node_module_a'
import 'node_module_b'
import 'node_module_c'

then you do this in the caller js file:

import 'all_the_things'

it will import all the nodes. Also, I found out if you put those imports in a parent file, loading only once, it will be viewed by other child components.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.