0

I made an export Javascript file like following:

export const A = 1;
export const B = 2;
export const C = 3;

And, I try to import that to another Javascript file like following:

import 'path/export_file.js'
console.log(A); // ReferenceError: A is not defined

I know I can fix it if I do the following:

import A from 'path/export_file.js'
// or
import { A, B, C } from 'path/export_file.js'

But I want to use like import 'path/export_file.js' Some modules just do import'path/file' and I can use all of exported from that module.

What should I do?

Or am I mistaken for something?

msm082919
  • 617
  • 8
  • 24
  • 1
    Not possible unless the module assigns to global properties, which should really be avoided since it defeats the whole point of a module system. You should use `import { A, B, C } from 'path/export_file.js'`. – CertainPerformance Jan 18 '21 at 06:31

1 Answers1

3

There are 2 things to know:

  1. You should know about Import/Export default & named in ES6

  2. As @CertainPerformance's mention, you have to use {} unless the module assigns to global properties.

    import { A, B, C } from 'path/export_file.js

  3. In case of mix both Default and Named, you can use * like this

    import * as Mix from 'path/export_file.js

Thanks @prosti's answer with a great answer.

enter image description here