I know that import {x} from 'foo'
is only importing specific object from other module, import x from 'foo'
is importing whole module as another object (same as const x = require('foo')
, but what import 'foo'
do?
Asked
Active
Viewed 168 times
1

Kokizzu
- 24,974
- 37
- 137
- 233
-
1https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only – VLAZ Oct 09 '20 at 09:31
-
2`import 'foo'` is the same as `require('foo')` - it imports `'foo'` but doesn't extract any of its exports. It just loads the file. Presumably that will run some code that is needed. E.g., if the `'foo'` module has a `console.log("loaded")` in its body, you'd see that in your log. There might be other things that are needed like setting up stuff or loading extra things that don't need to be exported. Although, *usually* seems like it's used for importing old code that isn't an ES6 module but was very quickly made into one. It's a bit of a hack. – VLAZ Oct 09 '20 at 09:35