Kuan
Kuan

Reputation: 11389

How to use ES6 module system in my case?

All:

I am pretty new to ES6 module system, say I have files like:

cmod.js

export var name = "hello2";

main.js

import name from './cmod.js';
console.log(name);

After I run transpile :

babel ./*.js  --out-dir js/ --watch

I wonder why the result is undefined? Could any I tried to find answer from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export

But it seems too complicated to me to match my case.

Thanks

Upvotes: 1

Views: 95

Answers (1)

Bergi
Bergi

Reputation: 665296

You are exporting a named export, but importing the default one. You should be using either

// cmod.js
export var name = "hello2";
// useful for multiple exports

// main.js
import { name } from './cmod.js';
console.log(name);

or

// cmod.js
export default "hello2";
// useful for single-value exports

// main.js
import name from './cmod.js';
console.log(name);

Upvotes: 4

Related Questions