msm082919
msm082919

Reputation: 707

How can I import javascript module without ReferenceError?

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?

Upvotes: 0

Views: 87

Answers (1)

user12582716
user12582716

Reputation:

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

Upvotes: 3

Related Questions