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?

1
  • 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'. Commented Jan 18, 2021 at 6:31

1 Answer 1

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

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

Comments

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.